Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
Little Hi and Little ho, after redeeming their favorite prizes, continued their trip to the United States, reasoning, who decided to take the train to the next city-the city is about to have a food festival!
But unfortunately, little hi and Little ho did not get a good train ticket-they could only take their journey on the most dilapidated train.
Not only that, because of the attraction of the food festival, many people have embarked on the same journey as small hi-ho, so a lot of people have met the same situation as little hi-ho-which has led to a very, very large number of people in this car, so that there is not enough space for everyone to have a place to sit down.
Small hi and small ho in the spirit of comity their mood-of course, because they bought is arrangement, honestly stay in the combination of two carriages. They thought they would be able to arrive at their destination in such a stable way, but it backfired, their cabin attendant is an obsessive-compulsive disorder, every other small will always have to clean the health, and the time is late at night, everyone is already asleep, this behavior will always wake up some people. And once some of the neighboring passengers were awakened by the majority, they quarreled with the attendant and made everyone sleep well.
The little Hi and Little Ho, who looked at it all, decided to use their algorithmic knowledge to help the obsessive-compulsive attendant--to clean up as much rubbish as possible without arguing with the passengers.
The carriages of small hi and small ho can be abstracted into a column of n positions, numbered 1 sequentially. N, and only one passenger is resting at each location. At the same time, there are some rubbish to be cleaned at each location, in which the amount of garbage in the I location is WI. The attendant can choose some of these locations for cleanup, but it is worth noting that once there are more than Q locations in the numbered consecutive m positions being selected in this clean-up (that is, the passengers at this m position are at least q+1 awakened), an unpleasant spat will occur. The task of small Hi and Little ho is to calculate which locations to clean, and to clean as much rubbish as possible without having to quarrel.
Tip One: No matter what the dynamic planning, you need a state transfer equation!
Hint Two: What do you think is wrong? Where did state compression go?
Input
Each test point (input file) has and has only one set of test data.
The first behavior of each set of test data is three positive integers n, m, and Q, meaning as described earlier.
The second behavior of each set of test data is n integers, W1 to WN, representing the number of garbage per location.
For 100% of data, meet n<=1000, 2<=m<=10,1<=q<=m, wi<=100
Output
For each set of test data, an integer ans is output that indicates the maximum number of garbage that a steward can sweep without an altercation.
-
Sample input
-
-
Sample output
-
201
The core idea of state compression is to use bits to represent the two states of the corresponding position, the problem is how to know which states are legal and which states are illegal, so before writing the code, we should consider the situation of the state transfer. or the processing of the initial state.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 Const intMax_n =1001;6 Const intMax_m =Ten;7 8 intN, M, Q;9 intW[max_n];Ten intdp[max_n][1<<Max_m]; One BOOLok[1<<Max_m]; A - voidinit () { -Memset (OK,false,sizeof(OK)); the intVal, CNT; - for(inti =0; I < (1<<M); ++i) { -val = i; CNT =0; - while(Val >0) { +CNT + = (val &1); -Val >>=1; + } AOk[i] = (CNT <=Q); at } - } - - voidsolve () { -Memset (DP,-1,sizeof(DP)); -memset (dp[0],0,sizeof(dp[0])); in for(inti =1; I <= N; ++i) { - for(intj =0; J < (1<<M); ++J)if(dp[i-1][J]! =-1) { to intS0 = ((j<<1) & ((1<<M)-1)); + intS1 = ((j<<1|1) & ((1<<M)-1)); -DP[I][S0] = max (Dp[i][s0], dp[i-1][j]); the if(Ok[s1]) { *DP[I][S1] = max (dp[i][s1], dp[i-1][J] +w[i]); $ }Panax Notoginseng } - } the intres =0; + for(inti =0; I < (1<<M); ++i) { Ares =Max (res, dp[n][i]); the } +cout << Res <<Endl; - } $ $ intMain () { - while(Cin >> N >> M >>Q) { - init (); the for(inti =1; I <= N; ++i) { -CIN >>W[i];Wuyi } the solve (); - } Wu return 0; -}
[Hihocoder] #1044: State compression • One