Main Topic
Given n locations, each position I has a value[i] value, which selects several positions, so that the number of selected positions in a continuous m position does not exceed Q, and the value and maximum scheme of all options are calculated, and the maximum value and the output are obtained.
Analysis
1, the enumeration can be used to enumerate all the possible circumstances, each time the enumeration succeeds, update the global variable maximum value. Time complexity O (2^n). Of course it will time out ...
//index is the location of Dfs search, Num_in_last_m is the number of previous M locations that have been selected,//Total_value The total value when searching to the current locationvoidDfsintIndexintNum_in_last_m,inttotal_value) { if(index = = N) {//Recursive exitMax_value = max_value > Total_value?Max_value:total_value; return; } //the position at the current index is not selectedDFS (index +1, Num_in_last_m, Total_value); if(Index <M) { if(Num_in_last_m <Q) {Used[index]=true;//Used[i] is used to indicate whether position I is selectedDFS (index +1, Num_in_last_m +1, Total_value +Wastes[index]); } } Else if(Num_in_last_m-used[index-m] <Q) {Used[index]=true; DFS (Index+1, Num_in_last_m-used[index-m] +1, Total_value +Wastes[index]); } //Backtracking!!! Used[index] =false;}
2, the use of dynamic planning method
DP[I][J] represents the maximum value that can be obtained when the first I position, position (i-m+1, i-m+2, ... i) is a binary representation of the selected case (0 means unchecked, 1 is checked) is J.
If the problem satisfies the optimal substructure: transition from dp[i-1][k] to dp[i][j], if DP[I][J] gets the maximum value, then dp[i-1][k] must also be the maximum of the state (I-1, k); The problem is not valid: from Dp[i-1][k] deduction to Dp[i][j ], do not care how the State (I-1,K) is obtained, only care about the final result.
After the DP array is exhausted, if you want to get the final result, you need to traverse dp[n]k to get the maximum value for all cases.
intMinValue (intAintb) { returnA < b?a:b;}intMain () {scanf (" %d%d%d", &n, &m, &Q); for(inti =0; i < N; i++) {scanf ("%d", &Wastes[i]); } memset (Used,false,sizeof(used)); Memset (DP,0,sizeof(DP)); for(inti =1; I <= N; i++){ //Enumeration State (I-1,J) All cases J (a total of 1 << minValue (M, i-1)) for(intj =0; J < (1<< MinValue (M, I-1)); J + +){ intTotal =0; //calculate how many of the previous M-1 positions are 1 intK = J & (1<< MinValue (M-1I1)) -1); while(k) { total+ = (K &1); K>>=1; } //deduced from the state (I-1, J) to (I, K)K = (J <<1) & (1<< MinValue (M, i))-1); //not selected at position IDP[I][K] = Dp[i][k] > Dp[i-1][J]? DP[I][K]: dp[i-1][j]; if(Total <Q) { //selected at position IK |=1; DP[I][K]= Dp[i][k] > Dp[i-1][J] + wastes[i-1]? DP[I][K]: dp[i-1][J] + wastes[i-1]; } }} max_value=0; //traverse seek Maximum value for(inti =0; I < (1<< MinValue (M, N)); i++) {Max_value= max_value > Dp[n][i]?Max_value:dp[n][i]; } printf ("%d\n", Max_value); return 0;}
hiho_1044 State Compression