# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
511523 |
2022-01-16T01:23:18 Z |
KoD |
Bajka (COCI20_bajka) |
C++17 |
|
258 ms |
3776 KB |
#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <class T> using Heap = std::priority_queue<T, vector<T>, std::greater<>>;
constexpr int inf = std::numeric_limits<int>::max() / 2;
bool setmin(int& x, const int y) {
if (x > y) {
x = y;
return true;
}
return false;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
vector<char> S(N), T(M);
for (auto& x : S) {
std::cin >> x;
}
for (auto& x : T) {
std::cin >> x;
}
vector dp(M, vector(N, inf));
Heap<tuple<int, int, int>> que;
const auto push = [&](const int m, const int i, const int d) {
if (setmin(dp[m][i], d)) {
que.emplace(d, m, i);
}
};
for (int i = 0; i < N; ++i) {
push(M - 1, i, 0);
}
while (!que.empty()) {
const auto [d, m, i] = que.top();
que.pop();
if (d > dp[m][i]) {
continue;
}
if (m > 0 and T[m] == S[i]) {
if (i > 0) {
push(m - 1, i - 1, d + 1);
}
if (i + 1 < N) {
push(m - 1, i + 1, d + 1);
}
}
for (int j = 0; j < N; ++j) {
if (S[i] == S[j]) {
push(m, j, d + std::abs(i - j));
}
}
}
int ans = inf;
for (int i = 0; i < N; ++i) {
if (T[0] == S[i]) {
setmin(ans, dp[0][i]);
}
}
std::cout << (ans == inf ? -1 : ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
824 KB |
Output is correct |
2 |
Correct |
55 ms |
756 KB |
Output is correct |
3 |
Correct |
43 ms |
716 KB |
Output is correct |
4 |
Correct |
49 ms |
752 KB |
Output is correct |
5 |
Correct |
4 ms |
316 KB |
Output is correct |
6 |
Correct |
2 ms |
312 KB |
Output is correct |
7 |
Correct |
117 ms |
1540 KB |
Output is correct |
8 |
Correct |
258 ms |
3776 KB |
Output is correct |
9 |
Correct |
99 ms |
2244 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
89 ms |
1164 KB |
Output is correct |