Submission #393703

# Submission time Handle Problem Language Result Execution time Memory
393703 2021-04-24T10:24:59 Z kshitij_sodani Exercise Deadlines (CCO20_day1problem2) C++14
17 / 25
1000 ms 16912 KB
//#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 aa,int bb){
	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;
		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 11 ms 720 KB Output is correct
2 Correct 11 ms 588 KB Output is correct
3 Correct 11 ms 588 KB Output is correct
4 Correct 11 ms 680 KB Output is correct
5 Correct 11 ms 664 KB Output is correct
6 Correct 12 ms 588 KB Output is correct
7 Correct 12 ms 716 KB Output is correct
8 Correct 12 ms 768 KB Output is correct
9 Correct 11 ms 772 KB Output is correct
10 Correct 11 ms 764 KB Output is correct
11 Correct 14 ms 716 KB Output is correct
12 Correct 12 ms 768 KB Output is correct
13 Correct 11 ms 588 KB Output is correct
14 Correct 12 ms 588 KB Output is correct
15 Correct 10 ms 688 KB Output is correct
16 Correct 11 ms 700 KB Output is correct
17 Correct 11 ms 688 KB Output is correct
18 Correct 11 ms 696 KB Output is correct
19 Correct 11 ms 588 KB Output is correct
20 Correct 12 ms 588 KB Output is correct
21 Correct 12 ms 588 KB Output is correct
22 Correct 13 ms 696 KB Output is correct
23 Correct 11 ms 684 KB Output is correct
24 Correct 13 ms 588 KB Output is correct
25 Correct 12 ms 688 KB Output is correct
26 Correct 11 ms 588 KB Output is correct
27 Correct 11 ms 596 KB Output is correct
28 Correct 11 ms 588 KB Output is correct
29 Correct 11 ms 588 KB Output is correct
30 Correct 10 ms 672 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 10 ms 588 KB Output is correct
33 Correct 10 ms 716 KB Output is correct
34 Correct 10 ms 692 KB Output is correct
35 Correct 2 ms 460 KB Output is correct
36 Correct 13 ms 588 KB Output is correct
37 Correct 11 ms 716 KB Output is correct
38 Correct 10 ms 684 KB Output is correct
39 Correct 13 ms 604 KB Output is correct
40 Correct 11 ms 692 KB Output is correct
41 Correct 2 ms 588 KB Output is correct
42 Correct 11 ms 588 KB Output is correct
43 Correct 3 ms 588 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 720 KB Output is correct
2 Correct 11 ms 588 KB Output is correct
3 Correct 11 ms 588 KB Output is correct
4 Correct 11 ms 680 KB Output is correct
5 Correct 11 ms 664 KB Output is correct
6 Correct 12 ms 588 KB Output is correct
7 Correct 12 ms 716 KB Output is correct
8 Correct 12 ms 768 KB Output is correct
9 Correct 11 ms 772 KB Output is correct
10 Correct 11 ms 764 KB Output is correct
11 Correct 14 ms 716 KB Output is correct
12 Correct 12 ms 768 KB Output is correct
13 Correct 11 ms 588 KB Output is correct
14 Correct 12 ms 588 KB Output is correct
15 Correct 10 ms 688 KB Output is correct
16 Correct 11 ms 700 KB Output is correct
17 Correct 11 ms 688 KB Output is correct
18 Correct 11 ms 696 KB Output is correct
19 Correct 11 ms 588 KB Output is correct
20 Correct 12 ms 588 KB Output is correct
21 Correct 12 ms 588 KB Output is correct
22 Correct 13 ms 696 KB Output is correct
23 Correct 11 ms 684 KB Output is correct
24 Correct 13 ms 588 KB Output is correct
25 Correct 12 ms 688 KB Output is correct
26 Correct 11 ms 588 KB Output is correct
27 Correct 11 ms 596 KB Output is correct
28 Correct 11 ms 588 KB Output is correct
29 Correct 11 ms 588 KB Output is correct
30 Correct 10 ms 672 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 10 ms 588 KB Output is correct
33 Correct 10 ms 716 KB Output is correct
34 Correct 10 ms 692 KB Output is correct
35 Correct 2 ms 460 KB Output is correct
36 Correct 13 ms 588 KB Output is correct
37 Correct 11 ms 716 KB Output is correct
38 Correct 10 ms 684 KB Output is correct
39 Correct 13 ms 604 KB Output is correct
40 Correct 11 ms 692 KB Output is correct
41 Correct 2 ms 588 KB Output is correct
42 Correct 11 ms 588 KB Output is correct
43 Correct 3 ms 588 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 954 ms 12244 KB Output is correct
47 Correct 931 ms 12316 KB Output is correct
48 Correct 890 ms 12244 KB Output is correct
49 Correct 873 ms 12208 KB Output is correct
50 Correct 944 ms 12228 KB Output is correct
51 Correct 893 ms 12208 KB Output is correct
52 Execution timed out 1057 ms 16912 KB Time limit exceeded
53 Halted 0 ms 0 KB -