#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define ordered_set(T) tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>
ordered_set(int) st,pos;
const int mxn = 2e5+10;
int arr[mxn];
std::priority_queue<pii,vector<pii>,less<pii>> pq;
int N;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<=N;i++)cin>>arr[i],pos.insert(i),pq.push(make_pair(arr[i],i));
ll ans = 0;
while(!pos.empty()){
int now = pos.size();
while(!pq.empty()&&pq.top().fs>=now){
if(pos.find(pq.top().sc) != pos.end()){
st.insert(pq.top().sc);
pq.pop();
}
}
/*
cout<<now<<":"<<endl;
for(auto &i:st)cout<<i<<' ';cout<<endl;
for(auto &i:pos)cout<<i<<' ';cout<<endl;
*/
if(st.empty()){
cout<<-1;
return 0;
}
int tar = *st.rbegin();
ans += now-pos.order_of_key(tar)-1;
pos.erase(tar);
st.erase(tar);
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
740 KB |
Output is correct |
2 |
Correct |
5 ms |
700 KB |
Output is correct |
3 |
Correct |
4 ms |
616 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
4 ms |
608 KB |
Output is correct |
6 |
Correct |
4 ms |
616 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
616 KB |
Output is correct |
10 |
Correct |
4 ms |
616 KB |
Output is correct |
11 |
Correct |
5 ms |
616 KB |
Output is correct |
12 |
Correct |
4 ms |
616 KB |
Output is correct |
13 |
Correct |
4 ms |
744 KB |
Output is correct |
14 |
Correct |
4 ms |
616 KB |
Output is correct |
15 |
Correct |
3 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
616 KB |
Output is correct |
17 |
Correct |
4 ms |
616 KB |
Output is correct |
18 |
Correct |
4 ms |
616 KB |
Output is correct |
19 |
Correct |
4 ms |
616 KB |
Output is correct |
20 |
Correct |
4 ms |
748 KB |
Output is correct |
21 |
Correct |
4 ms |
616 KB |
Output is correct |
22 |
Correct |
4 ms |
616 KB |
Output is correct |
23 |
Correct |
4 ms |
800 KB |
Output is correct |
24 |
Correct |
4 ms |
616 KB |
Output is correct |
25 |
Correct |
5 ms |
672 KB |
Output is correct |
26 |
Correct |
4 ms |
604 KB |
Output is correct |
27 |
Correct |
5 ms |
764 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
7 ms |
600 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
4 ms |
952 KB |
Output is correct |
33 |
Correct |
3 ms |
600 KB |
Output is correct |
34 |
Correct |
3 ms |
652 KB |
Output is correct |
35 |
Correct |
4 ms |
604 KB |
Output is correct |
36 |
Correct |
4 ms |
992 KB |
Output is correct |
37 |
Correct |
4 ms |
604 KB |
Output is correct |
38 |
Correct |
4 ms |
604 KB |
Output is correct |
39 |
Correct |
4 ms |
600 KB |
Output is correct |
40 |
Correct |
4 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
6 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
740 KB |
Output is correct |
2 |
Correct |
5 ms |
700 KB |
Output is correct |
3 |
Correct |
4 ms |
616 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
4 ms |
608 KB |
Output is correct |
6 |
Correct |
4 ms |
616 KB |
Output is correct |
7 |
Correct |
5 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
616 KB |
Output is correct |
9 |
Correct |
3 ms |
616 KB |
Output is correct |
10 |
Correct |
4 ms |
616 KB |
Output is correct |
11 |
Correct |
5 ms |
616 KB |
Output is correct |
12 |
Correct |
4 ms |
616 KB |
Output is correct |
13 |
Correct |
4 ms |
744 KB |
Output is correct |
14 |
Correct |
4 ms |
616 KB |
Output is correct |
15 |
Correct |
3 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
616 KB |
Output is correct |
17 |
Correct |
4 ms |
616 KB |
Output is correct |
18 |
Correct |
4 ms |
616 KB |
Output is correct |
19 |
Correct |
4 ms |
616 KB |
Output is correct |
20 |
Correct |
4 ms |
748 KB |
Output is correct |
21 |
Correct |
4 ms |
616 KB |
Output is correct |
22 |
Correct |
4 ms |
616 KB |
Output is correct |
23 |
Correct |
4 ms |
800 KB |
Output is correct |
24 |
Correct |
4 ms |
616 KB |
Output is correct |
25 |
Correct |
5 ms |
672 KB |
Output is correct |
26 |
Correct |
4 ms |
604 KB |
Output is correct |
27 |
Correct |
5 ms |
764 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
7 ms |
600 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
4 ms |
952 KB |
Output is correct |
33 |
Correct |
3 ms |
600 KB |
Output is correct |
34 |
Correct |
3 ms |
652 KB |
Output is correct |
35 |
Correct |
4 ms |
604 KB |
Output is correct |
36 |
Correct |
4 ms |
992 KB |
Output is correct |
37 |
Correct |
4 ms |
604 KB |
Output is correct |
38 |
Correct |
4 ms |
604 KB |
Output is correct |
39 |
Correct |
4 ms |
600 KB |
Output is correct |
40 |
Correct |
4 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
6 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
422 ms |
14900 KB |
Output is correct |
47 |
Correct |
400 ms |
14540 KB |
Output is correct |
48 |
Correct |
351 ms |
14796 KB |
Output is correct |
49 |
Correct |
333 ms |
14604 KB |
Output is correct |
50 |
Correct |
386 ms |
14908 KB |
Output is correct |
51 |
Correct |
367 ms |
14632 KB |
Output is correct |
52 |
Correct |
324 ms |
13524 KB |
Output is correct |
53 |
Correct |
341 ms |
13456 KB |
Output is correct |
54 |
Correct |
283 ms |
13272 KB |
Output is correct |
55 |
Correct |
221 ms |
13240 KB |
Output is correct |
56 |
Correct |
273 ms |
13364 KB |
Output is correct |
57 |
Correct |
292 ms |
13248 KB |
Output is correct |
58 |
Correct |
350 ms |
13236 KB |
Output is correct |
59 |
Correct |
346 ms |
13352 KB |
Output is correct |
60 |
Correct |
349 ms |
13364 KB |
Output is correct |
61 |
Correct |
307 ms |
13236 KB |
Output is correct |
62 |
Correct |
255 ms |
13764 KB |
Output is correct |
63 |
Correct |
286 ms |
13344 KB |
Output is correct |
64 |
Correct |
370 ms |
13412 KB |
Output is correct |
65 |
Correct |
368 ms |
13248 KB |
Output is correct |
66 |
Correct |
349 ms |
13376 KB |
Output is correct |
67 |
Correct |
316 ms |
13292 KB |
Output is correct |
68 |
Correct |
348 ms |
13428 KB |
Output is correct |
69 |
Correct |
391 ms |
13416 KB |
Output is correct |
70 |
Correct |
410 ms |
14436 KB |
Output is correct |
71 |
Correct |
385 ms |
14672 KB |
Output is correct |
72 |
Correct |
423 ms |
14420 KB |
Output is correct |
73 |
Correct |
402 ms |
14576 KB |
Output is correct |
74 |
Correct |
310 ms |
14508 KB |
Output is correct |
75 |
Correct |
375 ms |
14552 KB |
Output is correct |
76 |
Correct |
239 ms |
13688 KB |
Output is correct |
77 |
Correct |
376 ms |
22680 KB |
Output is correct |
78 |
Correct |
211 ms |
13276 KB |
Output is correct |
79 |
Correct |
241 ms |
13264 KB |
Output is correct |
80 |
Correct |
294 ms |
14036 KB |
Output is correct |
81 |
Correct |
375 ms |
22712 KB |
Output is correct |
82 |
Correct |
196 ms |
13308 KB |
Output is correct |
83 |
Correct |
237 ms |
13356 KB |
Output is correct |
84 |
Correct |
419 ms |
13464 KB |
Output is correct |
85 |
Correct |
403 ms |
13484 KB |
Output is correct |
86 |
Correct |
412 ms |
13360 KB |
Output is correct |
87 |
Correct |
155 ms |
13500 KB |
Output is correct |
88 |
Correct |
398 ms |
13304 KB |
Output is correct |