#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > l;
vector<int> s;
int t[800005];
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
return sum(v*2, tl, tm, l, min(r, tm))
+ sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr) {
t[v] += new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
t[v] = t[v*2] + t[v*2+1];
}
}
int main(){
int n;
cin >> n;
l.resize(n+1);
for(int i = 0; i < n; i++){
int a;
cin >> a;
s.push_back(a);
l[a].push_back(i);
update(1,0,n-1,i,1);
}
priority_queue<int> q;
long long res = 0;
for(int i = n-1; i >= 0; i--){
for(int j=0;j<l[i+1].size();j++)
{
q.push(l[i+1][j]);
}
if(q.empty())
{
cout << -1 << endl;
return 0;
}
int u=q.top();
q.pop();
// cout << res << endl;
int j = sum(1,0,n-1,0,u);
res += i-j+1;
update(1,0,n-1, u,-1);
}
cout << res << endl;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j=0;j<l[i+1].size();j++)
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
596 KB |
Output is correct |
2 |
Correct |
4 ms |
572 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
568 KB |
Output is correct |
5 |
Correct |
8 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
4 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
4 ms |
572 KB |
Output is correct |
11 |
Correct |
4 ms |
596 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
14 |
Correct |
5 ms |
608 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
596 KB |
Output is correct |
18 |
Correct |
6 ms |
520 KB |
Output is correct |
19 |
Correct |
5 ms |
596 KB |
Output is correct |
20 |
Correct |
5 ms |
596 KB |
Output is correct |
21 |
Correct |
5 ms |
596 KB |
Output is correct |
22 |
Correct |
5 ms |
572 KB |
Output is correct |
23 |
Correct |
5 ms |
596 KB |
Output is correct |
24 |
Correct |
4 ms |
596 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
596 KB |
Output is correct |
29 |
Correct |
4 ms |
644 KB |
Output is correct |
30 |
Correct |
4 ms |
576 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
552 KB |
Output is correct |
35 |
Correct |
5 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
596 KB |
Output is correct |
37 |
Correct |
4 ms |
596 KB |
Output is correct |
38 |
Correct |
4 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
596 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
628 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
312 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
596 KB |
Output is correct |
2 |
Correct |
4 ms |
572 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
568 KB |
Output is correct |
5 |
Correct |
8 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
4 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
4 ms |
572 KB |
Output is correct |
11 |
Correct |
4 ms |
596 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
14 |
Correct |
5 ms |
608 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
596 KB |
Output is correct |
18 |
Correct |
6 ms |
520 KB |
Output is correct |
19 |
Correct |
5 ms |
596 KB |
Output is correct |
20 |
Correct |
5 ms |
596 KB |
Output is correct |
21 |
Correct |
5 ms |
596 KB |
Output is correct |
22 |
Correct |
5 ms |
572 KB |
Output is correct |
23 |
Correct |
5 ms |
596 KB |
Output is correct |
24 |
Correct |
4 ms |
596 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
596 KB |
Output is correct |
29 |
Correct |
4 ms |
644 KB |
Output is correct |
30 |
Correct |
4 ms |
576 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
552 KB |
Output is correct |
35 |
Correct |
5 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
596 KB |
Output is correct |
37 |
Correct |
4 ms |
596 KB |
Output is correct |
38 |
Correct |
4 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
596 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
628 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
312 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
198 ms |
13240 KB |
Output is correct |
47 |
Correct |
213 ms |
13204 KB |
Output is correct |
48 |
Correct |
176 ms |
13156 KB |
Output is correct |
49 |
Correct |
176 ms |
13220 KB |
Output is correct |
50 |
Correct |
209 ms |
13216 KB |
Output is correct |
51 |
Correct |
199 ms |
13272 KB |
Output is correct |
52 |
Correct |
202 ms |
15352 KB |
Output is correct |
53 |
Correct |
242 ms |
15412 KB |
Output is correct |
54 |
Correct |
157 ms |
15336 KB |
Output is correct |
55 |
Correct |
144 ms |
15364 KB |
Output is correct |
56 |
Correct |
184 ms |
15332 KB |
Output is correct |
57 |
Correct |
168 ms |
15432 KB |
Output is correct |
58 |
Correct |
194 ms |
13096 KB |
Output is correct |
59 |
Correct |
229 ms |
13196 KB |
Output is correct |
60 |
Correct |
193 ms |
13152 KB |
Output is correct |
61 |
Correct |
186 ms |
13200 KB |
Output is correct |
62 |
Correct |
149 ms |
13112 KB |
Output is correct |
63 |
Correct |
171 ms |
13120 KB |
Output is correct |
64 |
Correct |
214 ms |
12960 KB |
Output is correct |
65 |
Correct |
219 ms |
13056 KB |
Output is correct |
66 |
Correct |
190 ms |
13152 KB |
Output is correct |
67 |
Correct |
185 ms |
12992 KB |
Output is correct |
68 |
Correct |
181 ms |
13092 KB |
Output is correct |
69 |
Correct |
206 ms |
13064 KB |
Output is correct |
70 |
Correct |
197 ms |
12996 KB |
Output is correct |
71 |
Correct |
218 ms |
13008 KB |
Output is correct |
72 |
Correct |
215 ms |
13096 KB |
Output is correct |
73 |
Correct |
212 ms |
12920 KB |
Output is correct |
74 |
Correct |
152 ms |
12980 KB |
Output is correct |
75 |
Correct |
221 ms |
12964 KB |
Output is correct |
76 |
Correct |
147 ms |
12580 KB |
Output is correct |
77 |
Correct |
148 ms |
12016 KB |
Output is correct |
78 |
Correct |
153 ms |
15316 KB |
Output is correct |
79 |
Correct |
145 ms |
13124 KB |
Output is correct |
80 |
Correct |
161 ms |
12716 KB |
Output is correct |
81 |
Correct |
151 ms |
12016 KB |
Output is correct |
82 |
Correct |
174 ms |
15460 KB |
Output is correct |
83 |
Correct |
139 ms |
13176 KB |
Output is correct |
84 |
Correct |
204 ms |
13260 KB |
Output is correct |
85 |
Correct |
206 ms |
13188 KB |
Output is correct |
86 |
Correct |
192 ms |
13104 KB |
Output is correct |
87 |
Correct |
117 ms |
12660 KB |
Output is correct |
88 |
Correct |
232 ms |
13172 KB |
Output is correct |