#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int arr[200002];
void checkIfAble(){
vector<int> v(arr+1, arr+n+1);
sort(v.begin(), v.end());
for(int i=1; i<=n; i++){
if(v[i-1] < i){
printf("-1");
exit(0);
}
}
}
vector<pair<int, int> > v;
set<int> st;
int tree[200002];
void add(int x, int val){
while(x){
tree[x] += val;
x -= x&(-x);
}
}
int sum(int x){
int ret = 0;
while(x<=n){
ret += tree[x];
x += x&(-x);
}
return ret;
}
int segTree[800002];
void initialize(int i, int l, int r){
if(l==r) segTree[i] = arr[l];
else{
int m = (l+r)>>1;
initialize(i*2, l, m), initialize(i*2+1, m+1, r);
segTree[i] = max(segTree[i*2], segTree[i*2+1]);
}
}
void setVal(int i, int l, int r, int idx, int val){
if(l==r) segTree[i] = val;
else{
int m = (l+r)>>1;
if(idx<=m) setVal(i*2, l, m, idx, val);
else setVal(i*2+1, m+1, r, idx, val);
segTree[i] = max(segTree[i*2], segTree[i*2+1]);
}
}
int maxIdx(int i, int l, int r, int val){
if(l==r) return l;
int m = (l+r)>>1;
if(segTree[i*2+1] >= val) return maxIdx(i*2+1, m+1, r, val);
return maxIdx(i*2, l, m, val);
}
ll ans;
int main(){
scanf("%d", &n);
for(int i=1; i<=n; i++) scanf("%d", &arr[i]), st.insert(i);
checkIfAble();
initialize(1, 1, n);
for(int i=1; i<=n; i++) add(i, 1);
for(int i=n; i>=1; i--){
int x = *st.rbegin();
int tmp = maxIdx(1, 1, n, i);
ans += sum(tmp)-1;
st.erase(tmp);
add(tmp, -1);
setVal(1, 1, n, tmp, 0);
}
printf("%lld", ans);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:75:13: warning: unused variable 'x' [-Wunused-variable]
75 | int x = *st.rbegin();
| ^
Main.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:68:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | for(int i=1; i<=n; i++) scanf("%d", &arr[i]), st.insert(i);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
768 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
768 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
4 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
640 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
2 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
768 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
640 KB |
Output is correct |
37 |
Correct |
4 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
640 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
5 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
42 |
Correct |
5 ms |
672 KB |
Output is correct |
43 |
Correct |
2 ms |
640 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
4 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
640 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
768 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
768 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
4 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
640 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
2 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
768 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
640 KB |
Output is correct |
37 |
Correct |
4 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
640 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
5 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
42 |
Correct |
5 ms |
672 KB |
Output is correct |
43 |
Correct |
2 ms |
640 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
225 ms |
13420 KB |
Output is correct |
47 |
Correct |
230 ms |
13584 KB |
Output is correct |
48 |
Correct |
177 ms |
13416 KB |
Output is correct |
49 |
Correct |
178 ms |
13464 KB |
Output is correct |
50 |
Correct |
221 ms |
13416 KB |
Output is correct |
51 |
Correct |
210 ms |
13416 KB |
Output is correct |
52 |
Correct |
337 ms |
13532 KB |
Output is correct |
53 |
Correct |
331 ms |
13432 KB |
Output is correct |
54 |
Correct |
256 ms |
13528 KB |
Output is correct |
55 |
Correct |
182 ms |
13380 KB |
Output is correct |
56 |
Correct |
256 ms |
13420 KB |
Output is correct |
57 |
Correct |
317 ms |
13544 KB |
Output is correct |
58 |
Correct |
372 ms |
13416 KB |
Output is correct |
59 |
Correct |
395 ms |
13500 KB |
Output is correct |
60 |
Correct |
380 ms |
13416 KB |
Output is correct |
61 |
Correct |
266 ms |
13416 KB |
Output is correct |
62 |
Correct |
183 ms |
13420 KB |
Output is correct |
63 |
Correct |
289 ms |
13420 KB |
Output is correct |
64 |
Correct |
384 ms |
13464 KB |
Output is correct |
65 |
Correct |
390 ms |
13416 KB |
Output is correct |
66 |
Correct |
311 ms |
13472 KB |
Output is correct |
67 |
Correct |
279 ms |
13452 KB |
Output is correct |
68 |
Correct |
347 ms |
13476 KB |
Output is correct |
69 |
Correct |
330 ms |
13536 KB |
Output is correct |
70 |
Correct |
273 ms |
13420 KB |
Output is correct |
71 |
Correct |
285 ms |
13416 KB |
Output is correct |
72 |
Correct |
276 ms |
13588 KB |
Output is correct |
73 |
Correct |
238 ms |
13420 KB |
Output is correct |
74 |
Correct |
192 ms |
13416 KB |
Output is correct |
75 |
Correct |
282 ms |
13416 KB |
Output is correct |
76 |
Correct |
99 ms |
11384 KB |
Output is correct |
77 |
Correct |
168 ms |
13420 KB |
Output is correct |
78 |
Correct |
175 ms |
13416 KB |
Output is correct |
79 |
Correct |
170 ms |
13448 KB |
Output is correct |
80 |
Correct |
98 ms |
11384 KB |
Output is correct |
81 |
Correct |
165 ms |
13416 KB |
Output is correct |
82 |
Correct |
168 ms |
13528 KB |
Output is correct |
83 |
Correct |
179 ms |
13384 KB |
Output is correct |
84 |
Correct |
502 ms |
13416 KB |
Output is correct |
85 |
Correct |
311 ms |
13464 KB |
Output is correct |
86 |
Correct |
310 ms |
13440 KB |
Output is correct |
87 |
Correct |
121 ms |
11384 KB |
Output is correct |
88 |
Correct |
309 ms |
13576 KB |
Output is correct |