#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX_N = 5e5;
pair<int, int> tmp[MAX_N];
int arr[MAX_N];
int res[MAX_N];
const int MAX_T = MAX_N+10;
int tree[MAX_T+1];
void update(int x, int val) {
x = MAX_T - x - 1;
tree[x] = val;
for (int k = x; k <= MAX_T; k += (k & -k))
tree[k] = max(tree[k], val);
}
int query(int x) {
x = MAX_T - x - 1;
int ret = -1;
for (int k = x; k > 0; k -= (k & -k))
ret = max(ret, tree[k]);
return ret;
}
int main(void) {
ios_base::sync_with_stdio(false), cin.tie(NULL);
memset(tree, -1, sizeof tree);
int N; cin >> N;
for (int i = 0; i < N; i ++) {
cin >> arr[i];
tmp[i].first = arr[i];
tmp[i].second = i;
}
sort(tmp, tmp + N);
int cur_idx = -1;
int prv_val = -1;
for (int i = 0; i < N; i ++) {
if (prv_val != tmp[i].first)
cur_idx++;
arr[tmp[i].second] = cur_idx;
prv_val = tmp[i].first;
}
cur_idx ++;
int last = 0;
memset(res, -1, sizeof res);
update(arr[0], 0);
for (int i = 1; i < N; i ++) {
if (arr[i] > arr[i-1]) {
int qidx = query(arr[i]);
if (qidx != -1) {
for (int k = qidx+1; k < i-last; k ++)
res[i-k-1] = k+1;
last = max(last, i-qidx-1);
}
else {
for (; last < i; last ++)
res[last] = i-last;
}
}
update(arr[i], i);
}
for (int i = 0; i < N; i ++)
cout << res[i] << ' ';
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4344 KB |
Output is correct |
3 |
Correct |
6 ms |
4492 KB |
Output is correct |
4 |
Correct |
7 ms |
4492 KB |
Output is correct |
5 |
Correct |
6 ms |
4492 KB |
Output is correct |
6 |
Correct |
6 ms |
4524 KB |
Output is correct |
7 |
Correct |
6 ms |
4524 KB |
Output is correct |
8 |
Correct |
6 ms |
4544 KB |
Output is correct |
9 |
Correct |
5 ms |
4544 KB |
Output is correct |
10 |
Correct |
5 ms |
4544 KB |
Output is correct |
11 |
Correct |
5 ms |
4544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4216 KB |
Output is correct |
2 |
Correct |
6 ms |
4344 KB |
Output is correct |
3 |
Correct |
6 ms |
4492 KB |
Output is correct |
4 |
Correct |
7 ms |
4492 KB |
Output is correct |
5 |
Correct |
6 ms |
4492 KB |
Output is correct |
6 |
Correct |
6 ms |
4524 KB |
Output is correct |
7 |
Correct |
6 ms |
4524 KB |
Output is correct |
8 |
Correct |
6 ms |
4544 KB |
Output is correct |
9 |
Correct |
5 ms |
4544 KB |
Output is correct |
10 |
Correct |
5 ms |
4544 KB |
Output is correct |
11 |
Correct |
5 ms |
4544 KB |
Output is correct |
12 |
Correct |
7 ms |
4544 KB |
Output is correct |
13 |
Correct |
8 ms |
4716 KB |
Output is correct |
14 |
Correct |
22 ms |
5204 KB |
Output is correct |
15 |
Correct |
41 ms |
5756 KB |
Output is correct |
16 |
Correct |
52 ms |
6232 KB |
Output is correct |
17 |
Correct |
140 ms |
9000 KB |
Output is correct |
18 |
Correct |
164 ms |
10396 KB |
Output is correct |
19 |
Correct |
209 ms |
12060 KB |
Output is correct |
20 |
Correct |
225 ms |
13328 KB |
Output is correct |
21 |
Correct |
228 ms |
13328 KB |
Output is correct |