제출 #310580

#제출 시각아이디문제언어결과실행 시간메모리
310580adrianbudauMatching (CEOI11_mat)C++17
100 / 100
307 ms42488 KiB
#include <iostream> #include <vector> #include <tuple> #include <algorithm> using namespace std; vector<int> invert(const vector<int> &P) { vector<int> where(P.size()); for (size_t i = 0; i < P.size(); ++i) where[P[i]] = i; return where; } bool check(int position, const vector<int> &prev, const vector<int> &next, const vector<int> &text, int needle) { int p = prev[position]; int n = next[position]; if (p != -1) { p += needle - position; if (text[p] > text[needle]) return false; } if (n != -1) { n += needle - position; if (text[n] < text[needle]) return false; } return true; } tuple<vector<int>, vector<int>, vector<int>> prefix(const vector<int>& P) { int N = P.size(); vector<int> where(N); for (int i = 0; i < N; ++i) where[P[i]] = i; vector<int> prev(N, -1), next(N, -1), pi(N, 0); for (int i = 0; i < N; ++i) { if (P[i] > 0) prev[i] = where[P[i] - 1]; if (P[i] + 1 < N) next[i] = where[P[i] + 1]; } for (int i = N - 1; i >= 0; --i) { auto p = prev[i]; auto n = next[i]; if (p != -1) { next[p] = n; } if (n != -1) { prev[n] = p; } } int k = 0; for (int i = 1; i < N; ++i) { while (k != 0 && !check(k, prev, next, P, i)) k = pi[k - 1]; if (check(k, prev, next, P, i)) ++k; pi[i] = k; } return {prev, next, pi}; } int main() { ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> P(N); for (int i = 0; i < N; ++i) { cin >> P[i]; --P[i]; } auto [prev, next, pi] = prefix(invert(P)); vector<int> T(M); for (int i = 0; i < M; ++i) { cin >> T[i]; } vector<int> answer; int k = 0; for (int i = 0; i < M; ++i) { while (k != 0 && !check(k, prev, next, T, i)) k = pi[k - 1]; if (check(k, prev, next, T, i)) ++k; if (k == int(P.size())) { answer.push_back(i - P.size() + 1); k = pi[k - 1]; } } cout << answer.size() << "\n"; for (auto &x : answer) cout << x + 1 << " "; cout << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...