Submission #171928

#TimeUsernameProblemLanguageResultExecution timeMemory
171928AlexPop28Matching (CEOI11_mat)C++11
81 / 100
1899 ms65540 KiB
#include <bits/stdc++.h> #define dbg() cerr << #define name(x) (#x) << ": " << (x) << ' ' << using namespace std; vector<int> Solve(int patt_size, const vector<int> &p, const vector<int> &small, const vector<int> &big) { int n = p.size(); vector<int> pi(patt_size); pi[0] = 0; auto Match = [&](int k, int i) { bool ret = true; int bg = big[k], sm = small[k]; ret &= bg == -1 || p[i] < p[i - k + bg]; ret &= sm == -1 || p[i] > p[i - k + sm]; return ret; }; vector<int> ans; int k = 0; for (int i = 1; i < n; ++i) { while (k != 0 && !Match(k, i)) k = pi[k - 1]; if (Match(k, i)) ++k; if (i < patt_size) pi[i] = k; if (k == patt_size && i >= 2 * patt_size) { ans.emplace_back(i - 2 * patt_size); } if (k == patt_size) k = pi[k - 1]; } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<int> p(n + m + 1); for (int i = 0; i < n; ++i) { int s; cin >> s; --s; p[s] = i; } p[n] = -1; for (int i = n + 1; i < n + m + 1; ++i) { cin >> p[i]; p[i] += n; } auto Comp = [&](int a, int b) { return p[a] < p[b]; }; vector<int> big(n, -1), small(n, -1); set<int, decltype(Comp)> vals(Comp); vals.emplace(0); for (int i = 1; i < n; ++i) { auto it = vals.emplace(i).first; if (it != vals.begin()) small[i] = *prev(it); if (next(it) != vals.end()) big[i] = *next(it); } auto ans0 = Solve(n, p, small, big); p[n] = (int)2e9; auto ans1 = Solve(n, p, small, big); vector<int> ans; int i = 0, j = 0; while (i < (int)ans0.size() && j < (int)ans1.size()) { if (ans0[i] < ans1[j]) { swap(i, j); swap(ans0, ans1); } while (j < (int)ans1.size() && ans1[j] < ans0[i]) ++j; if (j != (int)ans1.size()) { if (ans0[i] == ans1[j]) ans.emplace_back(ans0[i]), ++i, ++j; } } cout << ans.size() << endl; for (int &x : ans) { cout << 1 + x << ' '; } cout << endl; }
#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...