Submission #86112

# Submission time Handle Problem Language Result Execution time Memory
86112 2018-11-25T01:37:55 Z Shtef Maja (COCI18_maja) C++14
88 / 110
2000 ms 1132 KB
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;

int n, m, x1, y1, k;
ll dp[5][105][105], c[105][105], sol;
const ll inf = 1e15;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};

int main ()
{
scanf("%d %d %d %d %d", &n, &m, &x1, &y1, &k);
for(int i = 1 ; i <= n ; ++i){
	for(int j = 1 ; j <= m ; ++j){
		scanf("%lld", &c[i][j]);
	}
}
for(int i = 1 ; i <= n ; ++i){
	for(int j = 1 ; j <= m ; ++j){
		for(int v = 0 ; v < 5 ; ++v){
			dp[v][i][j] = -inf;
		}
	}
}
for(int i = 0 ; i < 5 ; ++i){
	dp[i][x1][y1] = 0;
}
ll maxi = 0;
for(int i = 1 ; i <= n ; ++i){
	for(int j = 1 ; j <= m ; ++j){
		for(int w = 0 ; w < 4 ; ++w){
			int nx = i + dx[w];
			int ny = j + dy[w];
			if(nx <= 0 || nx > n || ny < 0 || ny > m)
				continue;
			maxi = max(maxi, c[i][j] + c[nx][ny]);
		}
	}
}
for(int i = 1 ; i <= min(k, n * m) ; ++i){
	for(int j = 1 ; j <= n ; ++j){
		for(int v = 1 ; v <= m ; ++v){
			for(int w = 0 ; w < 4 ; ++w){
				int nx = j + dx[w];
				int ny = v + dy[w];
				if(nx <= 0 || nx > n || ny <= 0 || ny > m)
					continue;
				dp[i % 5][nx][ny] = max(dp[i % 5][nx][ny], dp[(i - 1 + 5) % 5][j][v] + c[nx][ny]);
				if(dp[i % 5][nx][ny] - dp[(i - 2 + 5) % 5][nx][ny] == dp[(i - 2 + 5) % 5][nx][ny] - dp[(i - 4 + 5) % 5][nx][ny]){
					if(dp[i % 5][nx][ny] - dp[(i - 2 + 5) % 5][nx][ny] > maxi)
						continue;
					sol = max(sol, 2LL * dp[(i - 4 + 5) % 5][nx][ny] + (ll)((k - 2 * (i - 4)) / 2) * (dp[i % 5][nx][ny] - dp[(i - 2 + 5) % 5][nx][ny]) - c[nx][ny]);
				}
			}
		}
	}
}
printf("%lld\n", (k <= n * m ? dp[k % 5][x1][y1] : sol));

return 0;
}

Compilation message

maja.cpp: In function 'int main()':
maja.cpp:18:6: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d %d %d %d %d", &n, &m, &x1, &y1, &k);
 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
maja.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &c[i][j]);
   ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 548 KB Output is correct
2 Correct 3 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 548 KB Output is correct
2 Correct 5 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 780 KB Output is correct
2 Correct 3 ms 780 KB Output is correct
3 Execution timed out 2067 ms 1132 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1132 KB Output is correct
2 Correct 968 ms 1132 KB Output is correct
3 Execution timed out 2060 ms 1132 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 804 ms 1132 KB Output is correct
2 Correct 1415 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 1132 KB Output is correct
2 Correct 86 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 1132 KB Output is correct
2 Correct 5 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 1132 KB Output is correct
2 Correct 5 ms 1132 KB Output is correct