Codeforces Round #584 E2. Rotate Columns (hard version)(状压DP)

https://codeforces.com/contest/1209/problem/E2

Meaning of the questions:

Given a matrix of N × M, you can run any number of rotating operation (i.e., overall upward or downward overall) of the numbers in each column. The maximum value of the output of each row to make any number of times after the rotation and operation.


Outline of Solution: see n ranges from small pressure DP can associate to form solutions, dp [i] set as the maximum value of the state i, the so-called state i, is in the binary i, a position represented by a the line maximum value found, 0 for no maximum is found, it may be a subset of the DP. Also worth noting that, at least we can find the answer in the fight min (n, m) column, so the first maximum per sort columns, take the first min (n, m) column just fine.


			for(int j=0;j<n;j++){
			stm[i].id=i;
			stm[i].w=tem;
		}
		sort(stm,stm+m);
		memset(use,0,sizeof(use));
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n&&i<m;i++){
			use[stm[i].id]=1;
		}
		for(int i=0;i<m;i++){
			if(use[i]==0){
				continue;
			}
			for(int k=0;k<n;k++){
				for(int j=0;j<(1<<n);j++){
					f1[j]=max(f1[j],f[j]);
					f[j]=dp[j];
					for(int kk=0;kk<n;kk++){
						int tem=(k+kk)%n;
						if((j&(1<<tem))){
							f[j]=max(f[j],f[j^(1<<tem)]+a[kk][i]);
						}
					}
				}
			}
			for(int j=0;j<(1<<n);j++){
				dp[j]=max(f1[j],f[j]);
				f1[j]=0;
				f[j]=0;
			}
		}
		printf("%d\n",dp[(1<<n)-1]);
	}
	return 0;
} 

  

Guess you like

Origin www.cnblogs.com/Zhi-71/p/11605494.html