title Address : http://hihocoder.com/problemset/problem/1044
algorithm idea: This problem can be seen as the deformation of the floor tiles, there is no obvious number of rows and state, but we can own the line and the state to buckle out. The first line is the number of N in the front (0,1,2 ... M-1), the second line is (2,3..M) ... Until the last act (n-m ... N). The number of States for each row is 2^m-1 (that is, the M position is either filled in 1, or 0 is filled). Available F[i][j] Indicates the maximum garbage value when the line I status is J. The final result is the maximum value in the state corresponding to the n-m line.
Previous blog on the state of the compression of the more detailed, you can see http://blog.csdn.net/lu597203933/article/details/44137277
Code:
#include <iostream> #include <memory.h> #include <time.h>using namespace std; #define NMAX 1000#define Mmax 1 << 10long long f[nmax][mmax];bool testcompatibility (int j, int M, int Q)//Determine if the state J is continuous M has greater than Q 1{int i = 0;int count = 0;while (i < M) {if (J & (1 << i)) {count + +;} i++;} if (Count > Q) return false;else return true;} Long Long Getfirstline (int j, int M, int *w)//get "first row" garbage total {int i = 0;long long rubbish = 0;while (i < M) {if (J & (1 << i)) {rubbish + = w[m-1-i];} i++;} return rubbish;} BOOL Testremaining (int j, int k, int m)//Determines whether the highest M-1 bit of j is equal to the low M-1 bit of k, i.e., the Judgment state J is compatible with the previous state k {k = k & ~ (1<< (M-1)); Place k M-1 Position 0k = k << 1;k = k | (J & 1); if (k = = j) return True;else return false;} int main () {int N, M, Q;while (cin >> N >> M >> Q) {int w[nmax];int sta[nmax];int TT = 0;int allstates = 1 << M;int I, j;for (i = 0; i < N; i++) cin >> W[i];memset (f, 0, sizeof (f)); long Long maxrubbish = 0;for (j = 0 ; J < Allstates; J + +) {if (Testcompatibility (J, M, Q)) {sta[tt++] = j; Save the available state to an array sta if it does not have this STA you need to traverse all states}}for (int j = 0; J < tt; J + +) F[0][j] = Getfirstline (Sta[j], M, W); Get the status of the first M units for (i = 1; I <= n-m; i++) {for (j = 0; J < tt; J + +) {for (int k = 0; k < tt; k++) {int aa = Sta[k] & ; ~ (1<< (M-1)); Place k M-1 Position 0AA = aa<< 1;aa = AA | (Sta[j] & 1);//if (Testremaining (Sta[j], sta[k], M))//Try not to use the function in the loop, otherwise the call cost will be large if (aa = = Sta[j]) {f[i][j] = max (f[i- 1][K],F[I][J]);}} if (((sta[j & 1) = = 1) {F[i][j] + = W[i+m-1];}} for (int j = 0; J < tt; J + +) Maxrubbish = max (Maxrubbish, F[n-m][j]); cout << maxrubbish << Endl;} return 0;} /*input:5 2 136 9 8510 8 338 98----
But this question my code to submit the time tle, the display time out, but I will use the time to output, than the online code http://hhfgeg.name/?id=158 still have less, but always tle, do not know why, have to know can comment. Thank you!
The following summarizes the general state compression of the solution steps
1 : Initialize, initialize first 0 The number of scenarios corresponding to each state of the row ( amount of garbage )
2 : By judging the first I the state of the row J whether it is compatible with the state of the previous line to seek F[i][j]
3: to find out the final result by the question
Algorithm Practice Series-hiho1044 State Compression two (pick up garbage)