QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116699#4273. Good Gamekshitij_sodani0 4ms5944kbC++144.2kb2023-06-29 20:25:262023-06-29 20:25:27

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-29 20:25:27]
  • 评测
  • 测评结果:0
  • 用时:4ms
  • 内存:5944kb
  • [2023-06-29 20:25:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
//#define endl '\n'


int n;

int tree[1000001];
vector<pair<int,int>> ans;
void u(int i,int j){
	while(i<=1e6){
		tree[i]+=j;
		i+=(i&-i);
	}
}
int s2(int i){
	int su=0;
	while(i>0){
		su+=tree[i];
		i-=(i&-i);
	}
	return su;
}
set<int> tt;
void pre(int i,int j){
	int x=s2(i)+1;
	int y=s2(j+1);
	//cout<<i<<":"<<j<<endl;
//	cout<<x<<","<<y<<endl;
	if((y-x+1)%2==0){
		for(int j=x;j<=y;j+=2){
			ans.pb({j,j+1});
		}
	}
	else{
		for(int j=x+1;j<=y;j+=2){
			if(j==x+1){
				ans.pb({j-1,j+1});
			}
			else{
				ans.pb({j,j+1});
			}
		}
	}
	auto jj=tt.lower_bound(i);
	vector<int> ee;
	while(jj!=tt.end()){
		if((*jj)>j){
			break;
		}
		ee.pb((*jj));
		jj++;
	}
	for(auto jj:ee){
		tt.erase(jj);
		u(jj+1,-1);
	}
}
void solve(vector<pair<int,pair<int,int>>> ss){
	if(ss[ss.size()/2].a==1){
			int ind=ss.size()/2;
			int ind2=ss.size()/2;
			while(ind>=0){
				pre(ss[ind].b.a,ss[ind2].b.b);
				ind--;
				ind2++;
			}
		}
		else{
			int ind=-1;
			for(int j=ss.size()/2;j>=0;j--){
				if(ss[j].a==1){
					ind=j;
					break;
				}
			}
			int ind2=-1;
			for(int j=ss.size()/2;j<ss.size();j++){
				if(ss[j].a==1){
					ind2=j;
					break;
				}
			}
			//cout<<ind<<":"<<ind2<<endl;
			if(ind==-1 or ind2==-1){
				cout<<-1<<endl;
				return ;
			}
			if(ind2-ind-1>=(ss.size()/2)){
				cout<<-1<<endl;
				return ;
			}
			int l=ind;
			int r=ss.size();
			r-=ind+1;
			vector<pair<int,int>> xx;
			vector<pair<int,int>> yy;
			int aa=ind2;
			int bb=ind2;
		//	cout<<l<<"?"<<r<<endl;
			while(r>l){
				pre(ss[aa].b.a,ss[bb].b.b);
				aa--;
				bb++;
				r-=2;
			}

			if(aa==bb){
				for(int j=ind+1;j<ss.size();j++){
					xx.pb(ss[j].b);
				}
			}
			else{
				xx.pb(ss[ind].b);
				for(int i=ind+1;i<=aa;i++){
					xx.pb(ss[i].b);
				}
				xx.pb({ss[aa+1].b.a,ss[bb-1].b.a});

				for(int i=bb;i<ss.size();i++){
					xx.pb(ss[i].b);
				}
			}
			//cout<<22<<endl;
			aa=ind;
			llo ind3=0;
			while(aa>=0){
				pre(ss[aa].b.a,xx[ind3].b);
				aa--;
				ind3++;
			}

		}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	vector<pair<int,pair<int,int>>> ss;
	
	string s;
	cin>>s;
	for(int i=0;i<n;i++){
		if(ss.size()==0){
			ss.pb({0,{i,i}});
		}
		else{
			if((s[i]-'A')==s[i-1]-'A'){
				ss[ss.size()-1].a|=1;
				ss[ss.size()-1].b.b+=1;
			}
			else{
				ss.pb({0,{i,i}});
			}
		}
	}

	for(int i=0;i<n;i++){
		u(i+1,1);
		tt.insert(i);
	}
	set<int> ee;
	for(int j=0;j<ss.size();j++){
		if(ss[j].a==1){
			ee.insert(j);
		}
	}
	/*for(auto j:ss){
		cout<<j.a<<":"<<j.b.a<<":"<<j.b.b<<endl;
	}*/
	//return 0;
	if(ss.size()%2==1){
		solve(ss);
	}
	else{
		if(ee.size()<2){
			cout<<-1<<endl;
			return 0;
		}
		int st=0;
		for(int i=0;i<ss.size();i+=2){
			int x=1;
			if(ss[(i+1)/2].a==1){
			}
			else{
				 int j=(*ee.begin());
				 if(j>=(i+1)/2){
				 	x=0;
				 }
				 else{
				 	auto k=ee.lower_bound((i+1)/2);
				 	if(k==ee.end()){
				 		x=0;
				 	}
				 	else{
				 		if((*k)>i){
				 			x=0;
				 		}
				 		else{
				 			if((*k)-j-1>=(i+1)/2){
				 				x=0;
				 			}
				 		}
				 	}
				 }
			}
			int y=1;
			pair<int,int> rr={i+1,ss.size()};
			int mid=(rr.a+rr.b)/2;
			if(ss[mid].a==1){
			}
			else{
				auto j=ee.lower_bound(i+1);
				if(j==ee.end()){
					y=0;
				}
				else{
					if((*j)>=mid){
						y=0;
					}
					else{
						auto k=ee.lower_bound(mid);
						if(k==ee.end()){
							y=0;
						}
						else if((*k)-(*j)-1>=((n-i-1)/2)){
							y=0;
						}
					}
				}
			}
			if(x==1 and y==1){
				st=1;
				//cout<<i<<"::::"<<endl;
				vector<pair<int,pair<int,int>>> xx;
				for(int j=0;j<=i;j++){
					xx.pb(ss[j]);
				}
				solve(xx);
				xx.clear();
				for(int j=i+1;j<ss.size();j++){
					xx.pb(ss[j]);
				}
				solve(xx);
				break;
			}
		}
		if(st==0){
			cout<<-1<<endl;
			return 0;
		}
	}


	cout<<ans.size()<<endl;
	for(auto j:ans){
		cout<<j.a<<" "<<j.b<<endl;
	}








	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 5512kb

input:

9
ABAABBBAA

output:

4
3 4
2 3
4 5
1 3

result:

wrong answer Integer 4 violates the range [2, 3]

Subtask #2:

score: 0
Wrong Answer

Test #51:

score: 6
Accepted
time: 1ms
memory: 5580kb

input:

299
ABABABABABABABABABABABABABABABABABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

-1

result:

ok no solution

Test #52:

score: 0
Accepted
time: 1ms
memory: 5492kb

input:

300
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAB...

output:

-1

result:

ok no solution

Test #53:

score: -6
Wrong Answer
time: 3ms
memory: 5524kb

input:

299
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

148
199 200
201 202
203 204
198 199
197 198
196 197
195 196
194 195
193 194
192 193
191 192
190 191
189 190
188 189
187 188
186 187
185 186
184 185
183 184
182 183
181 182
180 181
179 180
178 179
177 178
176 177
175 176
174 175
173 174
172 173
171 172
170 171
169 170
168 169
167 168
166 167
165 166
...

result:

wrong answer Integer 200 violates the range [2, 3]

Subtask #3:

score: 0
Wrong Answer

Test #102:

score: 0
Wrong Answer
time: 4ms
memory: 5944kb

input:

5998
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

2997
3995 3997
3998 3999
4000 4001
4002 4003
3994 3995
3993 3994
3992 3993
3991 3992
3990 3991
3989 3990
3988 3989
3987 3988
3986 3987
3985 3986
3984 3985
3983 3984
3982 3983
3981 3982
3980 3981
3979 3980
3978 3979
3977 3978
3976 3977
3975 3976
3974 3975
3973 3974
3972 3973
3971 3972
3970 3971
3969 ...

result:

wrong answer Integer 3997 violates the range [2, 3]

Subtask #4:

score: 0
Time Limit Exceeded

Test #153:

score: 0
Time Limit Exceeded

input:

999997
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

499997
666661 666662
666663 666664
666665 666666
666667 666668
666669 666670
666660 666661
666659 666660
666658 666659
666657 666658
666656 666657
666655 666656
666654 666655
666653 666654
666652 666653
666651 666652
666650 666651
666649 666650
666648 666649
666647 666648
666646 666647
666645 666646...

result: