QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116704#4273. Good Gamekshitij_sodani0 10ms5944kbC++144.2kb2023-06-29 20:31:002023-06-29 20:31:00

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:31:00]
  • 评测
  • 测评结果:0
  • 用时:10ms
  • 内存:5944kb
  • [2023-06-29 20:31:00]
  • 提交

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=y;j>=x;j-=2){
			ans.pb({j-1,j});
		}
	}
	else{
		for(int j=y;j>x;j-=2){
			if(j==x+2){
				ans.pb({j-2,j});
			}
			else{
				ans.pb({j-1,j});
			}
		}
	}
	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-j.a+1)<<endl;
	}








	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 1ms
memory: 5476kb

input:

9
ABAABBBAA

output:

4
3 2
4 2
2 2
1 3

result:

ok good solution!

Test #2:

score: -3
Wrong Answer
time: 1ms
memory: 3528kb

input:

13
ABABBABABBABA

output:

5
9 2
8 2
4 2
3 2
2 2

result:

wrong answer the string has not been fully deleted

Subtask #2:

score: 0
Wrong Answer

Test #51:

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

input:

299
ABABABABABABABABABABABABABABABABABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

-1

result:

ok no solution

Test #52:

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

input:

300
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAB...

output:

-1

result:

ok no solution

Test #53:

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

input:

299
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

148
203 2
201 2
199 2
198 2
197 2
196 2
195 2
194 2
193 2
192 2
191 2
190 2
189 2
188 2
187 2
186 2
185 2
184 2
183 2
182 2
181 2
180 2
179 2
178 2
177 2
176 2
175 2
174 2
173 2
172 2
171 2
170 2
169 2
168 2
167 2
166 2
165 2
164 2
163 2
162 2
161 2
160 2
159 2
158 2
157 2
156 2
155 2
154 2
153 2
15...

result:

wrong answer invalid operation on step #103: BA

Subtask #3:

score: 0
Wrong Answer

Test #102:

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

input:

5998
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

2997
4002 2
4000 2
3998 2
3995 3
3994 2
3993 2
3992 2
3991 2
3990 2
3989 2
3988 2
3987 2
3986 2
3985 2
3984 2
3983 2
3982 2
3981 2
3980 2
3979 2
3978 2
3977 2
3976 2
3975 2
3974 2
3973 2
3972 2
3971 2
3970 2
3969 2
3968 2
3967 2
3966 2
3965 2
3964 2
3963 2
3962 2
3961 2
3960 2
3959 2
3958 2
3957 2
3...

result:

wrong answer invalid operation on step #2002: AB

Subtask #4:

score: 0
Time Limit Exceeded

Test #153:

score: 0
Time Limit Exceeded

input:

999997
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

499997
666669 2
666667 2
666665 2
666663 2
666661 2
666660 2
666659 2
666658 2
666657 2
666656 2
666655 2
666654 2
666653 2
666652 2
666651 2
666650 2
666649 2
666648 2
666647 2
666646 2
666645 2
666644 2
666643 2
666642 2
666641 2
666640 2
666639 2
666638 2
666637 2
666636 2
666635 2
666634 2
66663...

result: