During the competition, I thought about putting n questions under pressure, but I couldn't pay attention to the order. At that time, I had a bad mental outlook and it was really frustrating.
In fact, another angle of this question is the arrangement of N numbers. If I perform pressure on N people and set a large cycle based on the question step by step, I will be the question at the moment, the previous I-1 question has been done, and then the person who has done it is in the status of J, J may be 1110 1101 1011 or something (assuming three questions have been done ), in this way, I can only keep the status in order. I only take the maximum value in this status. How can I arrange it in sequence?
So far... It seems that the problem is no problem, this practice repeats M/N times and finally run the remaining one, because every time I was considered only related to the I-1, you can use a rolling array. The trouble is that you need to clear the array each time. You can only say that the space and time cannot have both.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;double A[11][1010];int n,m;double dp[2][1<<11];int main(){ int t,kase=0; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); for (int i=0;i<n;i++){ for (int j=0;j<m;j++) scanf("%lf",&A[i][j]); } int ALL=1<<n; for (int i=0;i<=ALL;i++){ dp[0][i]=dp[1][i]=0; } for (int i=0;i<n;i++){ dp[0][1<<i]=A[i][0]; } int p=0; double ans=0; for (int i=1;i<m;i++){ p^=1; for (int j=0;j<=ALL;j++) dp[p][j]=0; for (int j=0;j<ALL;j++){ if (dp[p^1][j]==0) continue; for (int k=0;k<n;k++){ if ((1<<k)&j) continue; int nt=(1<<k)|j; if (nt==ALL-1) nt=0; dp[p][nt]=max(dp[p][nt],dp[p^1][j]+A[k][i]); if (i==m-1) ans=max(ans,dp[p][nt]); } } } printf("Case #%d: %.5lf\n",++kase,ans); }}
HDU 5045 pressure DP Shanghai Online Competition