Submission #89412

# Submission time Handle Problem Language Result Execution time Memory
89412 2018-12-13T20:49:37 Z kraljlavova1 Maja (COCI18_maja) C++11
110 / 110
475 ms 1368 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
const int MAXN=110;
int n,m,a,b,k;
ll P[MAXN][MAXN];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
ll dp[MAXN][MAXN][2],best[MAXN][MAXN];
bool valid(int x,int y){
	if(x<0||x>=n) return false;
	if(y<0||y>=m) return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	cin>>n>>m>>a>>b>>k;
	a--;b--;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>P[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			dp[i][j][0]=-1;
			dp[i][j][1]=-1;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			best[i][j]=-4e18;
			for(int d=0;d<4;d++){
				if(valid(i+dx[d],j+dy[d])){
					best[i][j]=max(best[i][j],P[i][j]+P[i+dx[d]][j+dy[d]]);
				}
			}
		}
	}
	dp[a][b][0]++;
	int kk=n*m;
	for(int c=0;c<min(k/2,n*m);c++){
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(dp[i][j][0]!=-1){
					int x,y;
					for(int d=0;d<4;d++){
						x=i+dx[d];
						y=j+dy[d];
						if(valid(x,y)){
							dp[x][y][1]=max(dp[x][y][1],dp[i][j][0]+P[x][y]);
						}
					}
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				dp[i][j][0]=dp[i][j][1];
				dp[i][j][1]=-1;
			}
		}
	}
	if(k/2<=n*m){
		ll sol=-4e18;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				sol=max(sol,2*dp[i][j][0]-P[i][j]);
			}
		}
		cout<<sol<<"\n";
	}
	else{
		ll sol=-4e18,x=(k-2*n*m)/2;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				sol=max(sol,2*dp[i][j][0]+x*best[i][j]-P[i][j]);
			}
		}
		cout<<sol<<"\n";
	}
	return 0;
}

Compilation message

maja.cpp: In function 'int main()':
maja.cpp:43:6: warning: unused variable 'kk' [-Wunused-variable]
  int kk=n*m;
      ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 444 KB Output is correct
2 Correct 2 ms 472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 476 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 572 KB Output is correct
2 Correct 2 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 788 KB Output is correct
2 Correct 2 ms 808 KB Output is correct
3 Correct 467 ms 1196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1196 KB Output is correct
2 Correct 292 ms 1196 KB Output is correct
3 Correct 475 ms 1244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 221 ms 1320 KB Output is correct
2 Correct 389 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 1364 KB Output is correct
2 Correct 29 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1364 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 1368 KB Output is correct
2 Correct 3 ms 1368 KB Output is correct