이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef LOCAL
#include "Debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n), pos(n), l(n), r(n);
for (int i = 0; i < n; i++)
{
cin >> pos[i];
a[--pos[i]] = i;
l[i] = i - 1;
r[i] = i + 1;
}
vector<int> b(m);
for (int &x : b)
cin >> x;
vector<int> prev(n, -1), next(n, -1);
for (int i = n - 1; i >= 0; i--)
{
int x = a[i];
if (l[x] >= 0)
prev[i] = pos[l[x]];
if (r[x] < n)
next[i] = pos[r[x]];
if (l[x] >= 0)
r[l[x]] = r[x];
if (r[x] < n)
l[r[x]] = l[x];
}
auto match = [&](vector<int> &a, int i, int j)
{
return (prev[j] < 0 || a[i] > a[i - j + prev[j]]) && (next[j] < 0 || a[i] < a[i - j + next[j]]);
};
vector<int> pre(n, -1);
for (int i = 0, j = -1; i < n; i++)
{
while (j >= 0 && !match(a, i, j))
j = j ? pre[j - 1] + 1 : -1;
pre[i] = j++;
}
vector<int> ans;
for (int i = 0, j = 0; i < m; i++)
{
while (j >= 0 && !match(b, i, j))
j = j ? pre[j - 1] + 1 : -1;
if (j == n - 1)
{
j = pre[j];
ans.push_back(i - n + 1);
}
j++;
}
cout << size(ans) << endl;
for (int x : ans)
cout << x + 1 << ' ';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |