# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
330310 | 2020-11-24T17:57:17 Z | phathnv | Bajka (COCI20_bajka) | C++11 | 19 ms | 896 KB |
#include <bits/stdc++.h> #define mp make_pair #define X first #define Y second #define taskname "Bajka" using namespace std; typedef long long ll; typedef pair <int, int> ii; const int N = 302; const int INF = 1e9 + 1; struct data{ int x, y, val; data(int _x, int _y, int _val){ x = _x; y = _y; val = _val; } }; bool operator < (const data &d1, const data &d2){ return d1.val > d2.val; } int n, m, dp[N][N]; char sca[N], fav[N]; void readInput(){ cin >> n >> m >> (sca + 1) >> (fav + 1); } void solve(){ for(int i = 0; i <= m; i++) for(int j = 0; j <= n; j++) dp[i][j] = INF; priority_queue <data> pq; for(int j = 1; j <= n; j++) if (fav[1] == sca[j]){ dp[1][j] = 0; pq.push(data(1, j, 0)); } while (!pq.empty()){ int x = pq.top().x; int y = pq.top().y; int val = pq.top().val; pq.pop(); if (dp[x][y] != val) continue; if (x == m){ cout << val; return; } for(int z = 1; z <= n; z++){ if (sca[z] == sca[y] && dp[x][z] > dp[x][y] + abs(y - z)){ dp[x][z] = dp[x][y] + abs(y - z); pq.push(data(x, z, dp[x][z])); } if (y != z && sca[z] == fav[x + 1] && dp[x + 1][z] > dp[x][y] + abs(y - z)){ dp[x + 1][z] = dp[x][y] + abs(y - z); pq.push(data(x + 1, z, dp[x + 1][z])); } } } cout << -1; } int main(){ if (fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); readInput(); solve(); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 364 KB | Output is correct |
2 | Correct | 1 ms | 748 KB | Output is correct |
3 | Correct | 1 ms | 364 KB | Output is correct |
4 | Correct | 1 ms | 748 KB | Output is correct |
5 | Correct | 1 ms | 748 KB | Output is correct |
6 | Correct | 1 ms | 748 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 19 ms | 896 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |