Submission #813583

# Submission time Handle Problem Language Result Execution time Memory
813583 2023-08-07T21:21:21 Z amirhoseinfar1385 Watermelon (INOI20_watermelon) C++17
81 / 100
33 ms 8336 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];
int vis[maxn],visind[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;
	vector<int>allcon;
	for(int i=1;i<=n;i++){
		if(vis[i]==0){
			allcon.push_back(i);
		}
	}
	for(int i=1;i<=n;i++){
		if(visind[i]==1){
			continue;
		}
		if(all[i]>=maxn){
			allv.push_back(i);
		}
	}
	int now=(int)allcon.size()-1;
	for(int i=0;i<(int)allv.size();i++){
		res[allv[i]]=allcon[now];
		now--;
	}
	now=0;
	int last=-1;
	vector<int>st;
	for(int j=1;j<=n;j++){
		if(all[j]==1){
			if(visind[j]==0){
				res[j]=allcon[now];
				now++;
			}
			int z=2;
			while((int)st.size()>0){
				auto x=st.back();
				if(all[x]==z){
					z++;
					if(visind[x]==0){
						res[x]=allcon[now];
						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";
	//	cout<<"wtf:\n";
	//	for(auto x:st){
	//		cout<<x<<'\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);
		}
		vector<int>tof;
		while((int)st.size()>0){
			auto x=*st.begin();
			st.erase(x);
			tof.push_back(x);
			//cout<<x<<" "<<j<<"\n";
			//cout<<x<<" "<<j<<" "<<res[x]<<" "<<res[link[j]]<<" "<<link[res[x]]<<" "<<link[j]<<"\n";
			int rx=res[x],rj=j;
			int lrx=link[res[x]],lj=link[j];
			res[lj]=rx;
			res[x]=rj;
			link[j]=lrx;
			link[rx]=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--;
			visind[x]=1;
			vis[j]=1;
			aval();
			//cout<<k<<" salam\n";
			if(bady(j)==1){
				return 1;
			}
			//swap(res[link[j]],res[x]);
			//swap(link[j],link[res[x]]);	
			res[lj]=rj;
			res[x]=rx;
			link[j]=lj;
			link[rx]=lrx;
			visind[x]=vis[j]=0;
			aval();
		}
		for(auto x:tof){
			st.insert(x);
		}
		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;
	}
//	cout<<"salam"<<endl;
	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:39:6: warning: unused variable 'last' [-Wunused-variable]
   39 |  int last=-1;
      |      ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2688 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 2600 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2564 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2688 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 2600 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2564 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2812 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2772 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 2 ms 2688 KB Output is correct
48 Correct 2 ms 2772 KB Output is correct
49 Correct 2 ms 2772 KB Output is correct
50 Correct 2 ms 2772 KB Output is correct
51 Correct 2 ms 2772 KB Output is correct
52 Correct 2 ms 2772 KB Output is correct
53 Correct 2 ms 2772 KB Output is correct
54 Correct 2 ms 2772 KB Output is correct
55 Correct 2 ms 2736 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 2772 KB Output is correct
58 Correct 2 ms 2772 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 2 ms 2772 KB Output is correct
61 Correct 2 ms 2812 KB Output is correct
62 Correct 2 ms 2816 KB Output is correct
63 Correct 2 ms 2772 KB Output is correct
64 Correct 2 ms 2680 KB Output is correct
65 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5264 KB Output is correct
2 Correct 9 ms 4968 KB Output is correct
3 Correct 11 ms 5204 KB Output is correct
4 Correct 9 ms 5080 KB Output is correct
5 Correct 10 ms 5200 KB Output is correct
6 Correct 10 ms 5288 KB Output is correct
7 Correct 11 ms 5268 KB Output is correct
8 Correct 12 ms 5276 KB Output is correct
9 Correct 12 ms 5264 KB Output is correct
10 Correct 10 ms 5264 KB Output is correct
11 Correct 20 ms 7736 KB Output is correct
12 Correct 20 ms 7620 KB Output is correct
13 Correct 20 ms 7616 KB Output is correct
14 Correct 21 ms 7620 KB Output is correct
15 Correct 17 ms 7360 KB Output is correct
16 Correct 17 ms 7364 KB Output is correct
17 Correct 16 ms 7372 KB Output is correct
18 Correct 17 ms 7372 KB Output is correct
19 Correct 16 ms 7364 KB Output is correct
20 Correct 17 ms 7372 KB Output is correct
21 Correct 17 ms 7300 KB Output is correct
22 Correct 18 ms 7372 KB Output is correct
23 Correct 16 ms 7416 KB Output is correct
24 Correct 20 ms 7868 KB Output is correct
25 Correct 19 ms 7876 KB Output is correct
26 Correct 20 ms 7904 KB Output is correct
27 Correct 20 ms 7852 KB Output is correct
28 Correct 20 ms 7844 KB Output is correct
29 Correct 20 ms 7772 KB Output is correct
30 Correct 7 ms 4188 KB Output is correct
31 Correct 10 ms 5268 KB Output is correct
32 Correct 20 ms 7144 KB Output is correct
33 Correct 20 ms 7728 KB Output is correct
34 Correct 3 ms 3284 KB Output is correct
35 Correct 5 ms 3716 KB Output is correct
36 Correct 10 ms 5260 KB Output is correct
37 Correct 19 ms 7620 KB Output is correct
38 Correct 19 ms 7740 KB Output is correct
39 Correct 20 ms 7688 KB Output is correct
40 Correct 18 ms 8336 KB Output is correct
41 Correct 21 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5264 KB Output is correct
2 Correct 9 ms 4968 KB Output is correct
3 Correct 11 ms 5204 KB Output is correct
4 Correct 9 ms 5080 KB Output is correct
5 Correct 10 ms 5200 KB Output is correct
6 Correct 10 ms 5288 KB Output is correct
7 Correct 11 ms 5268 KB Output is correct
8 Correct 12 ms 5276 KB Output is correct
9 Correct 12 ms 5264 KB Output is correct
10 Correct 10 ms 5264 KB Output is correct
11 Correct 20 ms 7736 KB Output is correct
12 Correct 20 ms 7620 KB Output is correct
13 Correct 20 ms 7616 KB Output is correct
14 Correct 21 ms 7620 KB Output is correct
15 Correct 17 ms 7360 KB Output is correct
16 Correct 17 ms 7364 KB Output is correct
17 Correct 16 ms 7372 KB Output is correct
18 Correct 17 ms 7372 KB Output is correct
19 Correct 16 ms 7364 KB Output is correct
20 Correct 17 ms 7372 KB Output is correct
21 Correct 17 ms 7300 KB Output is correct
22 Correct 18 ms 7372 KB Output is correct
23 Correct 16 ms 7416 KB Output is correct
24 Correct 20 ms 7868 KB Output is correct
25 Correct 19 ms 7876 KB Output is correct
26 Correct 20 ms 7904 KB Output is correct
27 Correct 20 ms 7852 KB Output is correct
28 Correct 20 ms 7844 KB Output is correct
29 Correct 20 ms 7772 KB Output is correct
30 Correct 7 ms 4188 KB Output is correct
31 Correct 10 ms 5268 KB Output is correct
32 Correct 20 ms 7144 KB Output is correct
33 Correct 20 ms 7728 KB Output is correct
34 Correct 3 ms 3284 KB Output is correct
35 Correct 5 ms 3716 KB Output is correct
36 Correct 10 ms 5260 KB Output is correct
37 Correct 19 ms 7620 KB Output is correct
38 Correct 19 ms 7740 KB Output is correct
39 Correct 20 ms 7688 KB Output is correct
40 Correct 18 ms 8336 KB Output is correct
41 Correct 21 ms 8276 KB Output is correct
42 Correct 11 ms 5288 KB Output is correct
43 Correct 9 ms 4952 KB Output is correct
44 Correct 11 ms 5264 KB Output is correct
45 Correct 10 ms 5068 KB Output is correct
46 Correct 9 ms 5076 KB Output is correct
47 Correct 9 ms 4968 KB Output is correct
48 Correct 9 ms 5080 KB Output is correct
49 Correct 12 ms 5276 KB Output is correct
50 Correct 11 ms 5268 KB Output is correct
51 Correct 11 ms 5264 KB Output is correct
52 Correct 21 ms 7836 KB Output is correct
53 Correct 22 ms 7876 KB Output is correct
54 Correct 16 ms 7372 KB Output is correct
55 Correct 17 ms 7392 KB Output is correct
56 Correct 19 ms 7364 KB Output is correct
57 Correct 20 ms 7876 KB Output is correct
58 Correct 20 ms 7904 KB Output is correct
59 Correct 20 ms 7884 KB Output is correct
60 Correct 20 ms 7872 KB Output is correct
61 Correct 20 ms 8264 KB Output is correct
62 Correct 20 ms 8272 KB Output is correct
63 Correct 21 ms 7940 KB Output is correct
64 Correct 21 ms 8052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2688 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 2600 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2564 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2812 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2772 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 2 ms 2688 KB Output is correct
48 Correct 2 ms 2772 KB Output is correct
49 Correct 2 ms 2772 KB Output is correct
50 Correct 2 ms 2772 KB Output is correct
51 Correct 2 ms 2772 KB Output is correct
52 Correct 2 ms 2772 KB Output is correct
53 Correct 2 ms 2772 KB Output is correct
54 Correct 2 ms 2772 KB Output is correct
55 Correct 2 ms 2736 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 2772 KB Output is correct
58 Correct 2 ms 2772 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 2 ms 2772 KB Output is correct
61 Correct 2 ms 2812 KB Output is correct
62 Correct 2 ms 2816 KB Output is correct
63 Correct 2 ms 2772 KB Output is correct
64 Correct 2 ms 2680 KB Output is correct
65 Correct 3 ms 2772 KB Output is correct
66 Correct 10 ms 5264 KB Output is correct
67 Correct 9 ms 4968 KB Output is correct
68 Correct 11 ms 5204 KB Output is correct
69 Correct 9 ms 5080 KB Output is correct
70 Correct 10 ms 5200 KB Output is correct
71 Correct 10 ms 5288 KB Output is correct
72 Correct 11 ms 5268 KB Output is correct
73 Correct 12 ms 5276 KB Output is correct
74 Correct 12 ms 5264 KB Output is correct
75 Correct 10 ms 5264 KB Output is correct
76 Correct 20 ms 7736 KB Output is correct
77 Correct 20 ms 7620 KB Output is correct
78 Correct 20 ms 7616 KB Output is correct
79 Correct 21 ms 7620 KB Output is correct
80 Correct 17 ms 7360 KB Output is correct
81 Correct 17 ms 7364 KB Output is correct
82 Correct 16 ms 7372 KB Output is correct
83 Correct 17 ms 7372 KB Output is correct
84 Correct 16 ms 7364 KB Output is correct
85 Correct 17 ms 7372 KB Output is correct
86 Correct 17 ms 7300 KB Output is correct
87 Correct 18 ms 7372 KB Output is correct
88 Correct 16 ms 7416 KB Output is correct
89 Correct 20 ms 7868 KB Output is correct
90 Correct 19 ms 7876 KB Output is correct
91 Correct 20 ms 7904 KB Output is correct
92 Correct 20 ms 7852 KB Output is correct
93 Correct 20 ms 7844 KB Output is correct
94 Correct 20 ms 7772 KB Output is correct
95 Correct 7 ms 4188 KB Output is correct
96 Correct 10 ms 5268 KB Output is correct
97 Correct 20 ms 7144 KB Output is correct
98 Correct 20 ms 7728 KB Output is correct
99 Correct 3 ms 3284 KB Output is correct
100 Correct 5 ms 3716 KB Output is correct
101 Correct 10 ms 5260 KB Output is correct
102 Correct 19 ms 7620 KB Output is correct
103 Correct 19 ms 7740 KB Output is correct
104 Correct 20 ms 7688 KB Output is correct
105 Correct 18 ms 8336 KB Output is correct
106 Correct 21 ms 8276 KB Output is correct
107 Correct 11 ms 5288 KB Output is correct
108 Correct 9 ms 4952 KB Output is correct
109 Correct 11 ms 5264 KB Output is correct
110 Correct 10 ms 5068 KB Output is correct
111 Correct 9 ms 5076 KB Output is correct
112 Correct 9 ms 4968 KB Output is correct
113 Correct 9 ms 5080 KB Output is correct
114 Correct 12 ms 5276 KB Output is correct
115 Correct 11 ms 5268 KB Output is correct
116 Correct 11 ms 5264 KB Output is correct
117 Correct 21 ms 7836 KB Output is correct
118 Correct 22 ms 7876 KB Output is correct
119 Correct 16 ms 7372 KB Output is correct
120 Correct 17 ms 7392 KB Output is correct
121 Correct 19 ms 7364 KB Output is correct
122 Correct 20 ms 7876 KB Output is correct
123 Correct 20 ms 7904 KB Output is correct
124 Correct 20 ms 7884 KB Output is correct
125 Correct 20 ms 7872 KB Output is correct
126 Correct 20 ms 8264 KB Output is correct
127 Correct 20 ms 8272 KB Output is correct
128 Correct 21 ms 7940 KB Output is correct
129 Correct 21 ms 8052 KB Output is correct
130 Correct 2 ms 2644 KB Output is correct
131 Correct 1 ms 2644 KB Output is correct
132 Correct 1 ms 2644 KB Output is correct
133 Correct 33 ms 8124 KB Output is correct
134 Incorrect 33 ms 8084 KB Output isn't correct
135 Halted 0 ms 0 KB -