Submission #127570

#TimeUsernameProblemLanguageResultExecution timeMemory
127570VlatkoMaja (COCI18_maja)C++14
11 / 110
1367 ms760 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 = 1e9; 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; ll C[maxn][maxn]; // dp[k][i][j] = max sum for path to here after k steps ll cur[maxn][maxn]; ll nxt[maxn][maxn]; 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]); } } } } for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { cur[i][j] = nxt[i][j]; } } } void simulate() { for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { ll gains = -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) { 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; 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...