QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#303101#4273. Good GameC1942huangjiaxu0 44ms21748kbC++142.6kb2024-01-11 18:15:312024-01-11 18:15:33

Judging History

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

  • [2024-01-11 18:15:33]
  • 评测
  • 测评结果:0
  • 用时:44ms
  • 内存:21748kb
  • [2024-01-11 18:15:31]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,ln[N],m,a[N],c,s[N],Ln[N],M;
char S[N];
vector<pair<int,int> >ans;
void print(){
	printf("%d\n",ans.size());
	for(auto [x,y]:ans)printf("%d %d\n",x,y);
}
void add(int x,int y){
	ans.push_back({x,y});
}
void del(int u){
	int p=s[u-1]+1;
	while(ln[u]>4){
		add(p,3);
		ln[u]-=3;
	}
	if(ln[u]==4)add(p,2),add(p,2);
	else if(ln[u]==3)add(p,3);
	else add(p,2);
}
bool check(int l,int r){
	int mid=l+r>>1;
	int u=upper_bound(a+1,a+c+1,mid)-a-1;
	if(!u)return false;
	if(a[u]==mid)return true;
	if(a[u+1]>r)return false;
	if(mid-a[u]<a[u+1]-mid){
		if(r-a[u+1]<mid-a[u])return false;
	}else{
		if(a[u]-1<a[u+1]-mid)return false;
	}
	return true;
}
bool cutl(){
	int p=1;
	while(a[p]*2<=m&&!check(a[p]*2+1,m))++p;
	if(a[p]*2>m)return false;
	int u=a[p];
	del(u);
	int nm=u+1;
	for(int i=u-1,j=u+1;i;--i,++j){
		ln[i]+=ln[j];
		del(i);
		nm=j+1;
	}
	for(int i=nm;i<=m;++i)ln[i-nm+1]=ln[i];
	c=0,m=m-nm+1;
	for(int i=1;i<=m;++i)if(ln[i]>1)a[++c]=i;
	for(int i=1;i<=m;++i)s[i]=s[i-1]+ln[i];
	return true;
}
bool cutr(){
	int p=c;
	while(a[p]*2>m&&!check(1,m-2*(m-a[p])-1))--p;
	if(a[p]*2<=m)return false;
	int u=a[p];
	del(u);
	int nm=u-1;
	for(int i=u+1,j=u-1;i<=m;++i,--j){
		ln[j]+=ln[i];
		del(j);
		nm=j-1;
	}
	c=0,m=nm;
	for(int i=1;i<=m;++i)if(ln[i]>1)a[++c]=i;
	return true;
}
void Del(int c,int p){
	int l=p,r=p;
	for(;c;++r,--l,--c){
		ln[l-1]+=ln[r+1];
		del(l);
	}
	int nm=l;
	for(int i=l+1,j=r+1;j<=m;++i,++j){
		nm=i;
		ln[i]=ln[j];
	}
	m=nm,c=0;
	for(int i=1;i<=m;++i)if(ln[i]>1)a[++c]=i;
	for(int i=1;i<=m;++i)s[i]=s[i-1]+ln[i];
}
void solve(){
	assert(m&1);
	int o=1,mid=(m+1)/2;
	while(a[o+1]<=mid)++o;
	if(a[o]!=mid){
		if(mid-a[o]<a[o+1]-mid)Del(mid-a[o],a[o+1]);
		else Del(a[o+1]-mid,a[o]);
	}
	o=1,mid=(m+1)/2;
	while(a[o+1]<=mid)++o;
	assert(a[o]==mid);
	for(int i=a[o],j=a[o];i;--i,++j){
		ln[i-1]+=ln[j+1];
		del(i);
	}
	print();
}
int main(){
	scanf("%d%s",&n,S+1);
	for(int i=1,j;j=i,i<=n;i=j+1){
		while(j<n&&S[j+1]==S[i])++j;
		ln[++m]=j-i+1,s[m]=s[m-1]+ln[m];
		if(ln[m]>1)a[++c]=m;
	}
	a[c+1]=m+1;
	if(!c)return puts("-1"),0;
	if(!(m&1)){
		M=m;
		for(int i=1;i<=m;++i)Ln[i]=ln[i];
		if(cutl()){
			solve();
			return 0;
		}
		ans.clear();
		m=M,c=0;
		for(int i=1;i<=m;++i)ln[i]=Ln[i];
		for(int i=1;i<=m;++i)if(ln[i]>1)a[++c]=i;
		for(int i=1;i<=m;++i)s[i]=s[i-1]+ln[i];
		if(cutr()){
			solve();
			return 0;
		}
		puts("-1");
		return 0;
	}
	if(check(1,m))solve();
	else puts("-1");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

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

input:

9
ABAABBBAA

output:

4
3 2
2 2
2 2
1 3

result:

ok good solution!

Test #2:

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

input:

13
ABABBABABBABA

output:

6
4 2
3 2
5 2
4 2
2 3
1 2

result:

ok good solution!

Test #3:

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

input:

15
AABABAABABAABAB

output:

7
1 2
4 2
3 2
5 2
4 2
2 3
1 2

result:

ok good solution!

Test #4:

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

input:

15
ABAABBBABAABBAB

output:

7
3 2
2 2
2 2
1 2
4 2
2 3
1 2

result:

ok good solution!

Test #5:

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

input:

1
B

output:

-1

result:

ok no solution

Test #6:

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

input:

2
BB

output:

1
1 2

result:

ok good solution!

Test #7:

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

input:

7
AAAABBB

output:

3
1 2
1 2
1 3

result:

ok good solution!

Test #8:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

11
BBBBBBAAAAA

output:

4
1 3
1 3
1 3
1 2

result:

ok good solution!

Test #9:

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

input:

2
AB

output:

-1

result:

ok no solution

Test #10:

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

input:

14
BAAAAAAAAAAAAA

output:

-1

result:

ok no solution

Test #11:

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

input:

14
ABBABAABBABBAB

output:

7
2 2
1 2
4 2
5 2
2 2
2 2
1 2

result:

ok good solution!

Test #12:

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

input:

15
BAABABABBABBAAB

output:

6
2 2
6 2
5 2
4 3
3 3
1 3

result:

ok good solution!

Test #13:

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

input:

14
BABBBBBBBBBBAB

output:

6
3 3
3 3
3 2
3 2
2 2
1 2

result:

ok good solution!

Test #14:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

15
BABAAAAAAAAABAB

output:

6
4 3
4 3
4 3
3 2
2 2
1 2

result:

ok good solution!

Test #15:

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

input:

14
BBBABAAAAAAABA

output:

6
1 3
3 3
3 2
3 2
2 2
1 2

result:

ok good solution!

Test #16:

score: 0
Accepted
time: 0ms
memory: 5692kb

input:

15
AAAABABAAAAABAB

output:

7
1 2
1 2
4 3
4 2
3 2
2 2
1 2

result:

ok good solution!

Test #17:

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

input:

14
BAAABABAAAABAB

output:

6
2 3
5 2
5 2
4 2
3 2
1 3

result:

ok good solution!

Test #18:

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

input:

15
ABAAAABABBBBABA

output:

7
3 2
3 2
5 2
5 2
4 2
2 3
1 2

result:

ok good solution!

Test #19:

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

input:

14
BABAAABAAAABAB

output:

6
4 3
5 2
5 2
3 3
2 2
1 2

result:

ok good solution!

Test #20:

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

input:

15
BABBABABBBBBBAB

output:

6
3 2
2 2
4 3
4 3
3 2
1 3

result:

ok good solution!

Test #21:

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

input:

14
BABAAAABABBBAB

output:

6
10 3
4 2
4 2
3 2
2 3
1 2

result:

ok good solution!

Test #22:

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

input:

15
BABAAAAAABABAAB

output:

6
13 2
4 3
4 3
3 2
2 2
1 3

result:

ok good solution!

Test #23:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

14
BABBBBBABAAAAA

output:

6
3 3
3 2
2 2
1 2
1 3
1 2

result:

ok good solution!

Test #24:

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

input:

15
BABAAAABABAAAAA

output:

7
4 2
4 2
3 2
2 2
1 2
1 3
1 2

result:

ok good solution!

Test #25:

score: 0
Accepted
time: 0ms
memory: 3892kb

input:

15
BAABABAABAABBAA

output:

7
2 2
1 2
3 2
4 2
2 2
2 2
1 3

result:

ok good solution!

Test #26:

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

input:

15
AABAABBAABAABAB

output:

7
1 2
6 2
7 2
4 2
4 2
2 3
1 2

result:

ok good solution!

Test #27:

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

input:

15
AABABBABAABBABB

output:

6
1 2
3 2
5 2
4 3
2 3
1 3

result:

ok good solution!

Test #28:

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

input:

15
BAABBABBAABABBA

output:

6
2 2
1 3
8 2
4 2
2 3
1 3

result:

ok good solution!

Test #29:

score: -3
Runtime Error

input:

15
BBAABBABABABBAA

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #51:

score: 6
Accepted
time: 0ms
memory: 5816kb

input:

299
ABABABABABABABABABABABABABABABABABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

-1

result:

ok no solution

Test #52:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

300
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAB...

output:

-1

result:

ok no solution

Test #53:

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

input:

299
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

147
96 3
96 3
96 2
95 2
94 2
93 2
92 2
91 2
90 2
89 2
88 2
87 2
86 2
85 2
84 2
83 2
82 2
81 2
80 2
79 2
78 2
77 2
76 2
75 2
74 2
73 2
72 2
71 2
70 2
69 2
68 2
67 2
66 2
65 2
64 2
63 2
62 2
61 2
60 2
59 2
58 2
57 2
56 2
55 2
54 2
53 2
52 2
51 2
50 2
49 2
97 3
97 3
96 2
95 2
94 2
93 2
92 2
91 2
90 2
8...

result:

ok good solution!

Test #54:

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

input:

299
BBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

147
1 3
1 3
96 3
95 2
94 2
93 2
92 2
91 2
90 2
89 2
88 2
87 2
86 2
85 2
84 2
83 2
82 2
81 2
80 2
79 2
78 2
77 2
76 2
75 2
74 2
73 2
72 2
71 2
70 2
69 2
68 2
67 2
66 2
65 2
64 2
63 2
62 2
61 2
60 2
59 2
58 2
57 2
56 2
55 2
54 2
53 2
52 2
51 2
50 2
49 2
97 3
97 2
96 2
95 2
94 2
93 2
92 2
91 2
90 2
89 ...

result:

ok good solution!

Test #55:

score: 0
Accepted
time: 0ms
memory: 5716kb

input:

297
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAABBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBAABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA

output:

148
74 2
73 2
73 2
72 2
71 2
70 2
69 2
68 2
67 2
66 2
65 2
64 2
63 2
62 2
61 2
60 2
59 2
58 2
57 2
56 2
55 2
54 2
53 2
52 2
51 2
50 2
49 2
48 2
47 2
46 2
45 2
44 2
43 2
42 2
41 2
40 2
39 2
38 2
37 2
36 2
35 2
34 2
33 2
32 2
31 2
30 2
29 2
28 2
27 2
26 2
25 2
24 2
23 2
22 2
21 2
20 2
19 2
18 2
17 2
1...

result:

ok good solution!

Test #56:

score: -6
Runtime Error

input:

300
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABAAAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABBBAAABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:


result:


Subtask #3:

score: 0
Runtime Error

Test #102:

score: 7
Accepted
time: 1ms
memory: 3948kb

input:

5998
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

2997
1996 2
1996 2
1995 2
1994 2
1993 2
1992 2
1991 2
1990 2
1989 2
1988 2
1987 2
1986 2
1985 2
1984 2
1983 2
1982 2
1981 2
1980 2
1979 2
1978 2
1977 2
1976 2
1975 2
1974 2
1973 2
1972 2
1971 2
1970 2
1969 2
1968 2
1967 2
1966 2
1965 2
1964 2
1963 2
1962 2
1961 2
1960 2
1959 2
1958 2
1957 2
1956 2
1...

result:

ok good solution!

Test #103:

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

input:

5999
BBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

2998
1 3
1 2
1996 2
1996 2
1995 2
1994 2
1993 2
1992 2
1991 2
1990 2
1989 2
1988 2
1987 2
1986 2
1985 2
1984 2
1983 2
1982 2
1981 2
1980 2
1979 2
1978 2
1977 2
1976 2
1975 2
1974 2
1973 2
1972 2
1971 2
1970 2
1969 2
1968 2
1967 2
1966 2
1965 2
1964 2
1963 2
1962 2
1961 2
1960 2
1959 2
1958 2
1957 2
...

result:

ok good solution!

Test #104:

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

input:

5998
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

2998
1499 3
1498 3
1497 2
1496 2
1495 2
1494 2
1493 2
1492 2
1491 2
1490 2
1489 2
1488 2
1487 2
1486 2
1485 2
1484 2
1483 2
1482 2
1481 2
1480 2
1479 2
1478 2
1477 2
1476 2
1475 2
1474 2
1473 2
1472 2
1471 2
1470 2
1469 2
1468 2
1467 2
1466 2
1465 2
1464 2
1463 2
1462 2
1461 2
1460 2
1459 2
1458 2
1...

result:

ok good solution!

Test #105:

score: -7
Runtime Error

input:

5998
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #153:

score: 9
Accepted
time: 42ms
memory: 16784kb

input:

999997
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

499996
333328 3
333328 3
333327 2
333326 2
333325 2
333324 2
333323 2
333322 2
333321 2
333320 2
333319 2
333318 2
333317 2
333316 2
333315 2
333314 2
333313 2
333312 2
333311 2
333310 2
333309 2
333308 2
333307 2
333306 2
333305 2
333304 2
333303 2
333302 2
333301 2
333300 2
333299 2
333298 2
33329...

result:

ok good solution!

Test #154:

score: 0
Accepted
time: 36ms
memory: 20748kb

input:

999998
BBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

499996
1 3
1 3
333328 3
333328 3
333327 2
333326 2
333325 2
333324 2
333323 2
333322 2
333321 2
333320 2
333319 2
333318 2
333317 2
333316 2
333315 2
333314 2
333313 2
333312 2
333311 2
333310 2
333309 2
333308 2
333307 2
333306 2
333305 2
333304 2
333303 2
333302 2
333301 2
333300 2
333299 2
333298...

result:

ok good solution!

Test #155:

score: 0
Accepted
time: 44ms
memory: 21748kb

input:

999997
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

499998
249999 2
249998 2
249998 2
249997 2
249996 2
249995 2
249994 2
249993 2
249992 2
249991 2
249990 2
249989 2
249988 2
249987 2
249986 2
249985 2
249984 2
249983 2
249982 2
249981 2
249980 2
249979 2
249978 2
249977 2
249976 2
249975 2
249974 2
249973 2
249972 2
249971 2
249970 2
249969 2
24996...

result:

ok good solution!

Test #156:

score: -9
Runtime Error

input:

999998
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:


result: