이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e6 + 5;
int s[maxn]; // povodne pole
int p[maxn]; // p[i] = relativna vyska i-teho schodiku
int nx[maxn], pv[maxn]; // polia pre linked list. Indexujeme do nich poradim. Cize na zaciatku su vsetky prvky zlinkovane v poradi 0, ..., n-1.
int dist_pv[maxn], dist_nx[maxn]; // o kolko dozadu sa nachadza prvok ktory je tesne mensi/vacsi ako ja?
int h[maxn]; // vysky budov
int f[maxn]; // failure funkcia pre KMP
bool ok(int i, int j, int *text) // mozeme posunut index j v patterne ak dalsi znak je text[i]?
{
if ((!dist_pv[j+1] || text[i-dist_pv[j+1]] < text[i]) && (!dist_nx[j+1] || text[i-dist_nx[j+1]] > text[i])) return true;
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> s[i];
p[--s[i]] = i;
nx[i] = i+1, pv[i] = i-1;
}
for (int i = n-1; i >= 0; i--)
{
if (pv[p[i]] != -1) // najblizsi mensi prvok v mojom prefixe
{
dist_pv[i] = i - s[pv[p[i]]]; // p[i] je hodnota mojho prvku, pv[p[i]] je najblizsi mensi
nx[pv[p[i]]] = nx[p[i]];
}
if (nx[p[i]] != n) // najblizsi vacsi prvok v mojom prefixe
{
dist_nx[i] = i - s[nx[p[i]]]; // p[i] je hodnota mojho prvku, nx[p[i]] je najblizsi vacsi
pv[nx[p[i]]] = pv[p[i]];
}
}
f[0] = -1;
for (int i = 1, j = f[0]; i < n; i++)
{
while (j != -1 && !ok(i, j, p)) j = f[j];
if (ok(i, j, p)) j++;
f[i] = j;
}
vector<int> ans;
for (int i = 0, j = -1; i < m; i++)
{
cin >> h[i];
while (j != -1 && !ok(i, j, h)) j = f[j];
if (ok(i, j, h)) j++;
if (j == n-1)
{
ans.push_back(i-(n-1));
j = f[j];
}
}
cout << ans.size() << "\n";
for (int i : ans) cout << i+1 << " ";
cout << "\n";
return 0;
}
# | 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... |