Submission #127572

#TimeUsernameProblemLanguageResultExecution timeMemory
127572VlatkoMaja (COCI18_maja)C++14
99 / 110
2050 ms640 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const ll inf = 1e18; const int maxn = 100; int di[4] = {-1, 0, 1, 0}; int dj[4] = {0, 1, 0, -1}; int N, M, A, B; ll K, T; vector<vector<ll>> C, cur, nxt; // dp[k][i][j] = max sum for path to here after k steps void dp_step() { for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { nxt[i][j] = -inf; for (int d = 0; d < 4; ++d) { int i1 = i + di[d], j1 = j + dj[d]; if (i1 >= 0 && i1 < N && j1 >= 0 && j1 < M) { nxt[i][j] = max(nxt[i][j], cur[i1][j1] + C[i][j]); } } } } swap(cur, nxt); } void simulate() { for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { ll gains = 0; for (int d = 0; d < 4; ++d) { int i1 = i + di[d], j1 = j + dj[d]; if (i1 >= 0 && i1 < N && j1 >= 0 && j1 < M) { gains = max(gains, C[i1][j1] + C[i][j]); } } cur[i][j] += gains * (K - 2*T) / 2; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M >> A >> B >> K; --A, --B; T = N * M * 2; C.assign(N, vector<ll>(M)); cur.assign(N, vector<ll>(M)); nxt.assign(N, vector<ll>(M)); for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { cin >> C[i][j]; cur[i][j] = -inf; } } cur[A][B] = 0; if (2*T >= K) { for (int i = 0; i < K; ++i) { dp_step(); } } else { // T steps, simulate going back and forth, T steps again for (int i = 0; i < T; ++i) { dp_step(); } simulate(); for (int i = 0; i < T; ++i) { dp_step(); } } cout << cur[A][B] << '\n'; }
#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...