Submission #151867

#TimeUsernameProblemLanguageResultExecution timeMemory
151867forestryksMatching (CEOI11_mat)C++14
0 / 100
316 ms27636 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; #define rep(i, n) for (int (i) = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define f first #define s second const int MAXN = 1e6 + 5; int m, n; int p[MAXN]; int a[MAXN * 2]; int up[MAXN]; int down[MAXN]; int pf[MAXN]; void calc() { set<pii> s; fill(up, up + m, -1); fill(down, down + m, -1); for (int i = 0; i < m; ++i) { auto it = s.lower_bound({a[i], 0}); if (it != s.end()) { up[i] = it->s; } if (it != s.begin()) { down[i] = prev(it)->s; } s.insert({a[i], i}); } } int main() { FAST_IO; cin >> m >> n; rep(i, m) { cin >> p[i]; p[i]--; } rep(i, m) { a[p[i]] = i; } a[m] = -1; rep(i, n) { cin >> a[m + 1 + i]; } calc(); pf[0] = 0; for (int i = 1; i < n + m + 1; ++i) { int j = pf[i - 1]; while (j > 0) { if (j == m) { j = pf[j - 1]; continue; } int u = up[j]; int d = down[j]; if ((d != -1 && a[i - j + 1 + d] > a[i]) || (u != -1 && a[i - j + 1 + u] < a[i])) { j = pf[j - 1]; continue; } pf[i] = j + 1; break; } if (j == 0) pf[i] = 1; } // rep(i, n + m + 1) { // cout << a[i] << ' '; // } // cout << endl; // rep(i, m) { // cout << down[i] << ' '; // } // cout << endl; // rep(i, m) { // cout << up[i] << ' '; // } // cout << endl; // rep(i, n + m + 1) { // cout << pf[i] << ' '; // } // cout << endl; vector<int> ans; for (int i = m + 1 + m - 1; i < n + m + 1; ++i) { if (pf[i] == m) { ans.push_back(i - m + 1 - m - 1); } } cout << ans.size() << endl; for (auto it : ans) { cout << it + 1 << ' '; } 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...