답안 #813578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
813578 2023-08-07T20:56:22 Z amirhoseinfar1385 Watermelon (INOI20_watermelon) C++17
31 / 100
21 ms 8696 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
int all[maxn],n,k,rishe=0,revcnt[maxn],link[maxn],res[maxn],cnt[maxn];
vector<int>par[maxn];
void cre(){
	for(int i=1;i<=n;i++){
		for(auto y:par[i]){
		//	cout<<i<<" "<<y<<"\n";
			revcnt[y]++;
		}
	//	cout<<i<<" "<<par[i]<<"\n";
	}
}

int aval(){
	vector<int>allv;
	for(int i=1;i<=n;i++){
		if(all[i]>=maxn){
			allv.push_back(i);
		}
	}
	vector<int>w(n+1);
	int now=n;
	for(int i=0;i<(int)allv.size();i++){
		res[allv[i]]=now;
		w[now]=i;
		now--;
	}
	now=1;
	int last=-1;
	vector<int>st;
	for(int j=1;j<=n;j++){
		if(all[j]==1){
			res[j]=now;
			now++;
			int z=2;
			while((int)st.size()>0){
				auto x=st.back();
				if(all[x]==z){
					z++;
					res[x]=now;
					w[now]=x;
					st.pop_back();
					now++;
					continue;
				}
				break;
			}
		}	
		if(all[j]!=1&&all[j]<maxn){
			st.push_back(j);
		}
	}
	if((int)st.size()>0){
		//cout<<-1<<"\n";
		return -1;
	}
	for(int i=1;i<=n;i++){
		link[res[i]]=i;
	}
	return 1;
}

int bady(int l=0){
	if(k==0){
		return 1;
	}
	set<int>st;
	for(int j=n;j>l;j--){
		for(auto y:par[link[j]]){
			st.erase(y);
		}
		if((int)st.size()>0){
			auto x=*st.begin();
			//cout<<x<<" "<<j<<"\n";
			//cout<<x<<" "<<j<<" "<<res[x]<<" "<<res[link[j]]<<" "<<link[res[x]]<<" "<<link[j]<<"\n";
			int rx=res[x],rj=res[link[j]];
			int lrx=link[res[x]],lj=link[j];
			res[link[j]]=rx;
			res[x]=rj;
			link[j]=lrx;
			link[res[x]]=lj;
			//swap(res[link[j]],res[x]);
			//swap(link[j],link[res[x]]);
			//cout<<x<<" "<<j<<" "<<res[x]<<" "<<res[link[j]]<<" "<<link[res[x]]<<" "<<link[res[j]]<<"\n";
			k--;
			//cout<<k<<" salam\n";
			if(bady(j)==1){
				return 1;
			}
			//swap(res[link[j]],res[x]);
			//swap(link[j],link[res[x]]);	
			res[link[j]]=rj;
			res[x]=rx;
			link[res[x]]=lrx;
			link[j]=lj;
		}
		st.insert(link[j]);
	}
	return -1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>all[i];
	}
	if(all[n]!=-1){
		cout<<-1<<"\n";
		return 0;
	}
	vector<int>fake;
	for(int i=1;i<=n;i++){
		if(all[i]==-1){
			fake.push_back(i);
		}
	}
	for(int i=0;i<(int)fake.size();i++){
		all[fake[i]]=maxn*2-i;
	}	
	rishe=fake[0];
	vector<int>v;
	for(int i=n;i>=1;i--){
		while((int)v.size()>0&&all[v.back()]<all[i]){
			v.pop_back();
		}
		if((int)v.size()!=0){
		//	cout<<i<<" "<<v.back()<<"\n";
			par[i].push_back(v.back());
		}
		v.push_back(i);
	}
	v.clear();
	for(int i=1;i<=n;i++){
		while((int)v.size()>0&&all[v.back()]<all[i]){
			v.pop_back();
		}
		if((int)v.size()!=0&&all[v.back()]>all[i]){
			par[i].push_back(v.back());
		}
		while((int)v.size()>0&&all[v.back()]<=all[i]){
			v.pop_back();
		}
		v.push_back(i);
	}
	cre();
	//cout<<"salam"<<endl;
	if(aval()==-1){
		//cout<<"ha"<<endl;
		cout<<-1<<'\n';
		return 0;
	}
	k--;
	if(bady()==-1){
		cout<<-1<<"\n";
		return 0;
	}
	for(int i=1;i<=n;i++){
		res[link[i]]=i;
	}
	for(int i=1;i<=n;i++){
		cout<<res[i]<<" ";
	}
}

// 3 2 1 1 2 3 4 3 2 1 -1

Compilation message

Main.cpp: In function 'int aval()':
Main.cpp:31:6: warning: unused variable 'last' [-Wunused-variable]
   31 |  int last=-1;
      |      ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Incorrect 1 ms 2644 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Incorrect 1 ms 2644 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 5264 KB Output is correct
2 Correct 9 ms 5204 KB Output is correct
3 Correct 10 ms 5252 KB Output is correct
4 Correct 9 ms 5208 KB Output is correct
5 Correct 11 ms 5296 KB Output is correct
6 Correct 11 ms 5280 KB Output is correct
7 Correct 11 ms 5264 KB Output is correct
8 Correct 11 ms 5264 KB Output is correct
9 Correct 11 ms 5256 KB Output is correct
10 Correct 10 ms 5264 KB Output is correct
11 Correct 20 ms 7924 KB Output is correct
12 Correct 20 ms 7908 KB Output is correct
13 Correct 20 ms 7860 KB Output is correct
14 Correct 20 ms 7860 KB Output is correct
15 Correct 17 ms 7604 KB Output is correct
16 Correct 16 ms 7608 KB Output is correct
17 Correct 16 ms 7632 KB Output is correct
18 Correct 16 ms 7632 KB Output is correct
19 Correct 16 ms 7632 KB Output is correct
20 Correct 16 ms 7608 KB Output is correct
21 Correct 17 ms 7760 KB Output is correct
22 Correct 20 ms 7748 KB Output is correct
23 Correct 16 ms 7608 KB Output is correct
24 Correct 20 ms 8148 KB Output is correct
25 Correct 20 ms 8184 KB Output is correct
26 Correct 20 ms 8228 KB Output is correct
27 Correct 19 ms 8116 KB Output is correct
28 Correct 20 ms 8212 KB Output is correct
29 Correct 20 ms 8184 KB Output is correct
30 Correct 7 ms 4308 KB Output is correct
31 Correct 10 ms 5316 KB Output is correct
32 Correct 17 ms 7460 KB Output is correct
33 Correct 19 ms 7868 KB Output is correct
34 Correct 3 ms 3284 KB Output is correct
35 Correct 5 ms 3832 KB Output is correct
36 Correct 10 ms 5340 KB Output is correct
37 Correct 19 ms 7884 KB Output is correct
38 Correct 19 ms 8140 KB Output is correct
39 Correct 19 ms 7936 KB Output is correct
40 Correct 18 ms 8532 KB Output is correct
41 Correct 21 ms 8696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 5264 KB Output is correct
2 Correct 9 ms 5204 KB Output is correct
3 Correct 10 ms 5252 KB Output is correct
4 Correct 9 ms 5208 KB Output is correct
5 Correct 11 ms 5296 KB Output is correct
6 Correct 11 ms 5280 KB Output is correct
7 Correct 11 ms 5264 KB Output is correct
8 Correct 11 ms 5264 KB Output is correct
9 Correct 11 ms 5256 KB Output is correct
10 Correct 10 ms 5264 KB Output is correct
11 Correct 20 ms 7924 KB Output is correct
12 Correct 20 ms 7908 KB Output is correct
13 Correct 20 ms 7860 KB Output is correct
14 Correct 20 ms 7860 KB Output is correct
15 Correct 17 ms 7604 KB Output is correct
16 Correct 16 ms 7608 KB Output is correct
17 Correct 16 ms 7632 KB Output is correct
18 Correct 16 ms 7632 KB Output is correct
19 Correct 16 ms 7632 KB Output is correct
20 Correct 16 ms 7608 KB Output is correct
21 Correct 17 ms 7760 KB Output is correct
22 Correct 20 ms 7748 KB Output is correct
23 Correct 16 ms 7608 KB Output is correct
24 Correct 20 ms 8148 KB Output is correct
25 Correct 20 ms 8184 KB Output is correct
26 Correct 20 ms 8228 KB Output is correct
27 Correct 19 ms 8116 KB Output is correct
28 Correct 20 ms 8212 KB Output is correct
29 Correct 20 ms 8184 KB Output is correct
30 Correct 7 ms 4308 KB Output is correct
31 Correct 10 ms 5316 KB Output is correct
32 Correct 17 ms 7460 KB Output is correct
33 Correct 19 ms 7868 KB Output is correct
34 Correct 3 ms 3284 KB Output is correct
35 Correct 5 ms 3832 KB Output is correct
36 Correct 10 ms 5340 KB Output is correct
37 Correct 19 ms 7884 KB Output is correct
38 Correct 19 ms 8140 KB Output is correct
39 Correct 19 ms 7936 KB Output is correct
40 Correct 18 ms 8532 KB Output is correct
41 Correct 21 ms 8696 KB Output is correct
42 Incorrect 11 ms 5240 KB Output isn't correct
43 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Incorrect 1 ms 2644 KB Output isn't correct
13 Halted 0 ms 0 KB -