Submission #171923

#TimeUsernameProblemLanguageResultExecution timeMemory
171923AlexPop28Matching (CEOI11_mat)C++11
73 / 100
2051 ms65540 KiB
#include <bits/stdc++.h> #define dbg() cerr << #define name(x) (#x) << ": " << (x) << ' ' << using namespace std; set<int> Solve(int patt_size, const vector<int> &p) { int n = p.size(); vector<int> pi(patt_size), big(patt_size, -1), small(patt_size, -1); 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; }; set<pair<int, int>> vals; vals.emplace(p[0], 0); set<int> ans; // for (int i = 0; i < n; ++i) dbg() p[i] << ' '; dbg() endl; int k = 0; for (int i = 1; i < n; ++i) { if (i < patt_size) { auto it = vals.emplace(p[i], i).first; if (it != vals.begin()) { small[i] = prev(it)->second; } if (next(it) != vals.end()) { big[i] = next(it)->second; } } // dbg() name(i) name(small[i]) name(big[i]) endl; 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(i - 2 * patt_size); } if (k == patt_size) k = pi[k - 1]; // dbg() name(i) name(pi[i]) endl; } 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 ans0 = Solve(n, p); p[n] = (int)2e9; auto ans1 = Solve(n, p); vector<int> ans; for (auto &x : ans0) { if (ans1.find(x) != ans1.end()) { ans.emplace_back(x); } } 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...