#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 10000000000
#define MAXN 55
typedef pair<ll int,ll int> pii;
ll int mapi[51][51][51][51];//mapi[i][j][ii][jj] é answer para a submatriz
ll int presum[MAXN][MAXN];
ll int dp[MAXN][MAXN];
ll int arr[MAXN][MAXN];
ll int kk;
void aa(){
ll int i,j,ii,jj;
for(i=0;i<50;i++){
for(j=0;j<50;j++){
for(ii=0;ii<=i;ii++){
for(jj=0;jj<=j;jj++){
mapi[i][j][ii][jj]=INF;
}
}
}
}
}
ll int getsum(ll int i,ll int j, ll int ii, ll int jj){
ll int ans=presum[i][j];
if(jj==0&&ii==0)return ans;
if(ii==0){
ans-=presum[i][jj-1];
return ans;
}
if(jj==0){
ans-=presum[ii-1][j];
return ans;
}
ans-=presum[i][jj-1];
ans-=presum[j][ii-1];
ans+=presum[ii-1][jj-1];
return ans;
}
void build(ll int i,ll int j,ll int ii,ll int jj){
if(i==ii&&j==jj){
mapi[i][j][i][j]=0;
return;
}
if(i==ii&&j==jj+1){
mapi[i][j][ii][jj] = arr[i][j]+arr[i][jj];
return;
}
if(j==jj&&i==ii+1){
mapi[i][j][ii][jj] = arr[i][j]+arr[ii][j];
return;
}
if(i==ii+1&&j==jj+1){
mapi[i][j][ii][jj] = arr[i][j]+arr[ii][jj]+arr[ii][j]+arr[i][jj];
mapi[i][j][ii][jj]*=2;
return;
}
ll int nii,njj,sum;
sum=getsum(i,j,ii,jj);
for(njj=j-1;njj>=jj;njj--){
//auto d=make_pair( make_pair(i,j), make_pair(ii,njj+1) );
if(mapi[i][j][ii][njj+1]==INF){
build(i,j,ii,njj+1);
}
kk = mapi[i][j][ii][njj+1]+sum;
//d=make_pair( make_pair(i,njj), make_pair(ii,jj) );
if(mapi[i][njj][ii][jj]==INF){
build(i,njj,ii,jj);
}
kk+=mapi[i][njj][ii][jj];
//d=make_pair( make_pair(i,j), make_pair(ii,jj) );
mapi[i][j][ii][jj] = min( mapi[i][j][ii][jj],kk );
}
//fazer o mesmo na horizontal
for(nii=i-1;nii>=ii;nii--){
//auto d=make_pair( make_pair(i,j), make_pair(nii+1,jj) );
if(mapi[i][j][nii+1][jj]==INF){
build(i,j,nii+1,jj);
}
kk = mapi[i][j][nii+1][jj]+sum; //if(i==1&&j==1&&ii==0&&jj==1)cout<<k<<"\n";
//d=make_pair( make_pair(nii,j), make_pair(ii,jj) );
if(mapi[nii][j][ii][jj]==INF){
build(nii,j,ii,jj);
}
kk+=mapi[nii][j][ii][jj]; //if(i==1&&j==1&&ii==0&&jj==1)cout<<k<<"\n";
//d=make_pair( make_pair(i,j), make_pair(ii,jj) );
mapi[i][j][ii][jj] = min( mapi[i][j][ii][jj],kk );
}
}
int main(){
ll int n,m,i,ii,jj,j,sum,k;
scanf("%lld %lld", &n, &m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){ dp[i][j]=INF;
scanf("%lld", &arr[i][j]);
if(i==0&&j==0){presum[i][j]=arr[0][0]; continue;}
if(i==0){
presum[i][j]=presum[i][j-1]+arr[i][j];
}else if(j==0){
presum[i][j]=presum[i-1][j]+arr[i][j];
}else{
presum[i][j]=presum[i-1][j]+presum[i][j-1]-presum[i-1][j-1]+arr[i][j];
}
}
}dp[0][0]=0;
aa();
for(i=0;i<n;i++){
for(j=0;j<m;j++){
sum=presum[i][j];
for(jj=j-1;jj>=0;jj--){
k=dp[i][jj]+sum;
//auto d=make_pair( make_pair(i,j), make_pair(0,jj+1) );
if(mapi[i][j][0][jj+1]==INF){
build(i,j,0,jj+1);
}
k+=mapi[i][j][0][jj+1];
dp[i][j]=min(dp[i][j], k);
}
//fazer o mesmo na horizontal
for(ii=i-1;ii>=0;ii--){
k=dp[ii][j]+sum;
//auto d=make_pair( make_pair(i,j), make_pair(ii+1,0) );
if(mapi[i][j][ii+1][0]==INF){
build(i,j,ii+1,0);
}
k+=mapi[i][j][ii+1][0]; //if(i==1&&j==1)cout<<k<<" "<<dp[i][j]<<endl<<endl;
dp[i][j]=min(dp[i][j], k);
}
}
}
/*
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<dp[i][j]<<" ";
}cout<<endl;
}cout<<endl;*/
cout<<dp[n-1][m-1]<<'\n';
return 0;
}
Compilation message
raisins.cpp: In function 'int main()':
raisins.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~
raisins.cpp:146:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &arr[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
34560 KB |
Output is correct |
2 |
Correct |
18 ms |
34432 KB |
Output is correct |
3 |
Correct |
19 ms |
34560 KB |
Output is correct |
4 |
Incorrect |
19 ms |
34432 KB |
Output isn't correct |
5 |
Incorrect |
19 ms |
34560 KB |
Output isn't correct |
6 |
Incorrect |
19 ms |
34560 KB |
Output isn't correct |
7 |
Incorrect |
19 ms |
34560 KB |
Output isn't correct |
8 |
Incorrect |
21 ms |
34560 KB |
Output isn't correct |
9 |
Incorrect |
25 ms |
34560 KB |
Output isn't correct |
10 |
Incorrect |
26 ms |
34560 KB |
Output isn't correct |
11 |
Incorrect |
26 ms |
34560 KB |
Output isn't correct |
12 |
Incorrect |
44 ms |
34560 KB |
Output isn't correct |
13 |
Incorrect |
70 ms |
34680 KB |
Output isn't correct |
14 |
Incorrect |
30 ms |
34688 KB |
Output isn't correct |
15 |
Incorrect |
70 ms |
34680 KB |
Output isn't correct |
16 |
Incorrect |
22 ms |
34560 KB |
Output isn't correct |
17 |
Incorrect |
38 ms |
34560 KB |
Output isn't correct |
18 |
Incorrect |
204 ms |
34680 KB |
Output isn't correct |
19 |
Incorrect |
298 ms |
34560 KB |
Output isn't correct |
20 |
Incorrect |
366 ms |
34560 KB |
Output isn't correct |