Submission #542237

#TimeUsernameProblemLanguageResultExecution timeMemory
542237IwanttobreakfreeRaisins (IOI09_raisins)C++98
100 / 100
132 ms26800 KiB
#include <iostream> #include <vector> using namespace std; const int N=51; int dp[N][N][N][N]; int pr[N][N]; int DP(int rl,int rr,int cl,int cr){ if(dp[rl][rr][cl][cr]!=-1)return dp[rl][rr][cl][cr]; int ans=1e9; int r=pr[rr][cr]-pr[rr][cl-1]-pr[rl-1][cr]+pr[rl-1][cl-1]; for(int i=rl;i<rr;i++){ ans=min(ans,DP(rl,i,cl,cr)+DP(i+1,rr,cl,cr)); } for(int i=cl;i<cr;i++){ ans=min(ans,DP(rl,rr,cl,i)+DP(rl,rr,i+1,cr)); } if(ans==1e9)ans=r=0; //cout<<rl<<' '<<rr<<' '<<cl<<' '<<cr<<' '<<ans+r<<'\n'; return dp[rl][rr][cl][cr]=r+ans; } int main(){ int n,m; cin>>n>>m; for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int q=0;q<N;q++)for(int k=0;k<N;k++)dp[i][j][q][k]=-1; for(int i=0;i<N;i++)for(int j=0;j<N;j++)pr[i][j]=0; int grid[n][m]; for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>grid[i][j]; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)pr[i][j]=pr[i-1][j]+pr[i][j-1]+grid[i-1][j-1]-pr[i-1][j-1]; cout<<DP(1,n,1,m); }
#Verdict Execution timeMemoryGrader output
Fetching results...