//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo n;
llo it[200001];
int tree2[200001];
void u(int i,int j){
while(i<=n){
tree2[i]+=j;
i+=(i&-i);
}
}
int ss2(int i){
int su=0;
while(i>0){
su+=tree2[i];
i-=(i&-i);
}
return su;
}
int tree[4*200001];
void build(int no,int l,int r){
if(l==r){
tree[no]=it[l];
}
else{
int mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
tree[no]=max(tree[no*2+1],tree[no*2+2]);
}
}
void update(int no,int l,int r,int i){
if(l==r){
tree[no]=-1;
}
else{
int mid=(l+r)/2;
if(i<=mid){
update(no*2+1,l,mid,i);
}
else{
update(no*2+2,mid+1,r,i);
}
tree[no]=max(tree[no*2+1],tree[no*2+2]);
}
}
int query(int no,int l,int r,int i){
if(l==r){
return l;
}
int mid=(l+r)/2;
if(tree[no*2+2]>=i){
return query(no*2+2,mid+1,r,i);
}
else{
return query(no*2+1,l,mid,i);
}
/*if(bb<l or r<aa){
return -1;
}
if(aa<=l and r<=bb){
return tree[no];
}
int mid=(l+r)/2;
return max(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));*/
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
vector<llo> ss;
set<llo> tt;
for(llo i=0;i<n;i++){
cin>>it[i];
it[i]--;
tt.insert(it[i]);
ss.pb(it[i]);
}
sort(ss.begin(),ss.end());
for(llo i=0;i<n;i++){
if(ss[i]<i){
cout<<-1<<endl;
return 0;
}
}
build(0,0,n-1);
for(int i=1;i<=n;i++){
u(i,1);
}
llo ans=0;
for(llo i=n-1;i>=0;i--){
//llo ind=n;
//continue;
llo ind=query(0,0,n-1,i);
/*for(int j=19;j>=0;j--){
if(ind-(1<<j)>=0){
//cout<<ind-(1<<j)<<":"<<query(0,0,n-1,ind-(1<<j),n-1)<<endl;
if(query(0,0,n-1,ind-(1<<j),n-1)>=i){
}
else{
//cout<<11<<endl;
ind-=(1<<j);
}
}
}*/
//ind--;
//cout<<ind<<":"<<endl;
//break;
ans+=ss2(n)-ss2(ind+1);
update(0,0,n-1,ind);
u(ind+1,-1);
//cout<<ind<<":"<<endl;
//break;
/*for(llo j=0;j<=i;j++){
if(it[j]==i){
ind=j;
}
}
for(llo j=ind;j<i;j++){
ans+=1;
swap(it[j],it[j+1]);
}
for(llo j=0;j<n;j++){
it[j]=min(it[j],i-1);
}*/
}
cout<<ans<<endl;
return 0;
/* for(llo i=0;i<n;i++){
cout<<it[i]<<",";
}
cout<<endl;
*/
/*llo ans=0;
for(auto j:tt){
llo ind=-1;
//cout<<j<<endl;
while(true){
llo st=0;
for(llo i=max((llo)0,ind);i<n;i++){
if(it[i]==j and i>j){
ind=i;
st=1;
break;
}
}
if(st==0){
break;
}
llo ind2=-1;
for(llo i=ind;i>=0;i--){
if(it[i]>j){
ind2=i;
break;
}
}
for(llo i=ind2;i<ind;i++){
swap(it[i],it[i+1]);
ans+=1;
}
ind-=1;
}
}*/
cout<<ans<<endl;
/*while(true){
if(ind==-1){
break;
}
for(llo i=ind;i>it[ind];i--){
swap(it[i],it[i-1]);
ans++;
}
cout<<ind<<endl;
for(llo i=0;i<n;i++){
cout<<it[i]<<":";
}
cout<<endl;
break;
}
cout<<ans<<endl;
*/
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
588 KB |
Output is correct |
3 |
Correct |
3 ms |
588 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
716 KB |
Output is correct |
12 |
Correct |
4 ms |
716 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
4 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
3 ms |
588 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
588 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
588 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
588 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
3 ms |
660 KB |
Output is correct |
28 |
Correct |
3 ms |
588 KB |
Output is correct |
29 |
Correct |
4 ms |
660 KB |
Output is correct |
30 |
Correct |
3 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
588 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
588 KB |
Output is correct |
39 |
Correct |
3 ms |
588 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
588 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
588 KB |
Output is correct |
3 |
Correct |
3 ms |
588 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
716 KB |
Output is correct |
12 |
Correct |
4 ms |
716 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
4 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
3 ms |
588 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
588 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
588 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
588 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
3 ms |
660 KB |
Output is correct |
28 |
Correct |
3 ms |
588 KB |
Output is correct |
29 |
Correct |
4 ms |
660 KB |
Output is correct |
30 |
Correct |
3 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
588 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
588 KB |
Output is correct |
39 |
Correct |
3 ms |
588 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
588 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
182 ms |
10976 KB |
Output is correct |
47 |
Correct |
179 ms |
10964 KB |
Output is correct |
48 |
Correct |
136 ms |
10908 KB |
Output is correct |
49 |
Correct |
139 ms |
10948 KB |
Output is correct |
50 |
Correct |
156 ms |
10944 KB |
Output is correct |
51 |
Correct |
144 ms |
10928 KB |
Output is correct |
52 |
Correct |
231 ms |
15664 KB |
Output is correct |
53 |
Correct |
231 ms |
16948 KB |
Output is correct |
54 |
Correct |
176 ms |
16816 KB |
Output is correct |
55 |
Correct |
132 ms |
16868 KB |
Output is correct |
56 |
Correct |
182 ms |
16856 KB |
Output is correct |
57 |
Correct |
187 ms |
16816 KB |
Output is correct |
58 |
Correct |
207 ms |
13616 KB |
Output is correct |
59 |
Correct |
208 ms |
13664 KB |
Output is correct |
60 |
Correct |
193 ms |
13644 KB |
Output is correct |
61 |
Correct |
156 ms |
13620 KB |
Output is correct |
62 |
Correct |
124 ms |
13600 KB |
Output is correct |
63 |
Correct |
169 ms |
13624 KB |
Output is correct |
64 |
Correct |
201 ms |
13440 KB |
Output is correct |
65 |
Correct |
199 ms |
13360 KB |
Output is correct |
66 |
Correct |
179 ms |
13432 KB |
Output is correct |
67 |
Correct |
152 ms |
13356 KB |
Output is correct |
68 |
Correct |
177 ms |
13388 KB |
Output is correct |
69 |
Correct |
201 ms |
13480 KB |
Output is correct |
70 |
Correct |
179 ms |
12892 KB |
Output is correct |
71 |
Correct |
179 ms |
12880 KB |
Output is correct |
72 |
Correct |
170 ms |
12856 KB |
Output is correct |
73 |
Correct |
157 ms |
12872 KB |
Output is correct |
74 |
Correct |
121 ms |
12808 KB |
Output is correct |
75 |
Correct |
165 ms |
12848 KB |
Output is correct |
76 |
Correct |
106 ms |
9520 KB |
Output is correct |
77 |
Correct |
91 ms |
7780 KB |
Output is correct |
78 |
Correct |
128 ms |
16816 KB |
Output is correct |
79 |
Correct |
121 ms |
13548 KB |
Output is correct |
80 |
Correct |
106 ms |
9392 KB |
Output is correct |
81 |
Correct |
90 ms |
7752 KB |
Output is correct |
82 |
Correct |
127 ms |
16868 KB |
Output is correct |
83 |
Correct |
117 ms |
13540 KB |
Output is correct |
84 |
Correct |
189 ms |
12984 KB |
Output is correct |
85 |
Correct |
184 ms |
12840 KB |
Output is correct |
86 |
Correct |
186 ms |
12848 KB |
Output is correct |
87 |
Correct |
114 ms |
10176 KB |
Output is correct |
88 |
Correct |
185 ms |
12896 KB |
Output is correct |