Submission #86102

#TimeUsernameProblemLanguageResultExecution timeMemory
86102ShtefMaja (COCI18_maja)C++14
11 / 110
1304 ms1080 KiB
#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 = 1e18; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; int main () { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> x1 >> y1 >> k; for(int i = 1 ; i <= n ; ++i){ for(int j = 1 ; j <= m ; ++j){ cin >> 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; } for(int i = 1 ; i <= min(k, 3 * n * m) ; ++i){ memset(dp[i % 5], -1, sizeof(dp[i % 5])); dp[i % 5][x1][y1] = 0; 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(i < 4) continue; for(int j = 1 ; j <= n ; ++j){ for(int v = 1 ; v <= m ; ++v){ //cout << dp[i % 5][j][v] << ' ' << dp[(i - 2 + 5) % 5][j][v] << ' ' << dp[(i - 4 + 5) % 5][j][v] << ' ' << j << ' ' << v << endl; if(dp[i % 5][j][v] - dp[(i - 2 + 5) % 5][j][v] == dp[(i - 2 + 5) % 5][j][v] - dp[(i - 4 + 5) % 5][j][v]){ //cout << i << " [ " << j << " ]" << "[ " << v << " ]" << endl; //if(2 * dp[(i - 4 + 5) % 5][j][v] + ((k - 2 * (i - 4)) / 2) * (dp[i % 5][j][v] - dp[(i - 2 + 5) % 5][j][v]) - c[j][v] > sol) // cout << dp[i % 5][j][v] << ' ' << dp[(i - 2 + 5) % 5][j][v] << ' ' << i << ' ' << j << ' ' << v << endl, system("pause"); sol = max(sol, 2 * dp[(i - 4 + 5) % 5][j][v] + ((k - 2 * (i - 4)) / 2) * (dp[i % 5][j][v] - dp[(i - 2 + 5) % 5][j][v]) - c[j][v]); } } } //system("pause"); } cout << max(sol, dp[k % 5][x1][y1]) << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...