Question:
Suppose there are n arrays A [m] (which can be considered as a [n] [m]).
A [m] indicates the state of each segment of an object divided into M segments. The element value of a [m] is 0 or 1, 0 indicates that this segment is unavailable, and 1 indicates that it is available.
Then, the available part of an object in a [m] is equal to the sum of all 1, which is S (A [m]) =.
Calculate a public solution B [m], and assume that it is S (B [m]) = B
If a [m] contains B [m], that is, B [m] is a subset of a [m], the B [m] part of a [m] can be used, and B.
Otherwise, if a [m] cannot completely contain B [m], a [m] is unavailable and its sum is 0.
B [m] is required to maximize the sum of N A [M.
Problem conversion:
Matlab code:
Function solve ()
[Leni, lenj] = size ();
B = zeros (1, lenj); % Set B
M = Leni; % A contains the number of rows of B
N = 0; % size of Set B
While 1
% It is better to change the 0 element in B to 1.
TRS = ones (1, lenj) * INF; % Save the reduce of m in various attempts
For I = 1: lenj
If B (I) = 0
% Suppose to change B (I) to 1
% Calculates the number of rows whose column I is 0 in a (that is, the number of rows whose column I does not contain the modified B), that is, the number of M reductions
Dm = size (find (A (:, I) = 0), 1 );
TRS (I) = DM;
End
End
% Calculate the minimum value in TRS
[DM, I] = min (TRS); % that is, changing B (I) to 1 can minimize the m reduction, which is DM
% Actual operation
If (m-DM) * (n + 1)> = m * n % if the change can indeed improve the target value, the actual modification is made.
% (After analysis, "> =" is better than ">" -- it provides an opportunity to cross the local optimal solution)
B (I) = 1;
N = n + 1;
M = m-DM;
A = a (A (:, I) = 1, :); % the row that does not contain the current B is deleted.
Else % otherwise, the local optimum is reached and the end is reached.
Break;
End
End
Disp ('result :');
Disp ('target value :');
Disp (M * n );
Disp ('B :');
Disp (B );
End
Test:
A =
1 0 1 0 0 1 1 1 1
1 1 1 0 1 1 0 1 1
0 1 1 0 1 0 1 1 1
1 1 1 0 1 0 1 1 1
1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 1
Result:
Target value:
20
B:
1 1 0 0 1 0 1 1