QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#713656#8523. Puzzle IILogingWA 1ms7788kbC++142.1kb2024-11-05 20:12:262024-11-05 20:12:26

Judging History

This is the latest submission verdict.

  • [2024-11-05 20:12:26]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 7788kb
  • [2024-11-05 20:12:26]
  • Submitted

answer

#include<cstdio>
#include<algorithm>
#define M 600005
using namespace std;
char A[M],B[M];
int L[M],R[M];
int Ans[M][2];
int sz;
void Swap(int &a,int &b,int &c,int &d){
	swap(R[L[a]],R[L[c]]);
	swap(L[R[b]],L[R[d]]);
	swap(L[a],L[c]);
	swap(R[b],R[d]);
	swap(a,c);swap(b,d);
}
int n,m;
void Add(int &x){
	x++;
	if(x>n)x-=n;
}
void Del(int &x){
	x--;
	if(x==0)x+=n;
}
bool flag;
void Addans(int p1,int p2){
	sz++;
//	printf("p1=%d p2=%d\n",p1,p2);
	if(flag){p1+=m;p2+=m;}
	if(p1>n)p1-=n;
	if(p2>n)p2-=n;
	Ans[sz][0]=p1;Ans[sz][1]=p2;
}
int main(){
	scanf("%d%d",&n,&m);
	scanf("%s",A+1);
	scanf("%s",B+1);
	for(int i=n+1;i<=n+n;i++)A[i]=B[i-n];
	for(int i=1;i<=n;i++){
		int lst=i-1,nxt=i+1;
		if(lst==0)lst=n;
		if(nxt==n+1)nxt=1;
		L[i]=lst;R[i]=nxt;
		L[i+n]=lst+n;R[i+n]=nxt+n;
	}
	if(m>n/2){flag=true;m=n-m;}
//	if(flag)puts("!");
	char st=A[1];
	int fr1=2,ed1=1+m,fr2=n+1,ed2=n+m;
	int p1=2,p2=1;
	for(int i=2;i<=n;i++){
//		printf("i=%d\n",i);
		if(i+m-1<=n){
			
			while(p1!=i){
				fr1=R[fr1];
				ed1=R[ed1];
				Add(p1);
			}
//			puts("A");
//			printf("fr1=%d\n",fr1);
			if(A[fr1]==st){
				fr1=R[fr1];
				ed1=R[ed1];
				Add(p1);
				continue;
			}
//			puts("SWA");
			while(A[fr2]!=st){
				fr2=R[fr2];
				ed2=R[ed2];
				Add(p2);
			}
			Addans(p1,p2);
			Swap(fr1,ed1,fr2,ed2);
		}else{
			while(p1+m-1!=i){
				fr1=R[fr1];
				ed1=R[ed1];
				Add(p1);
			}
//			printf("p1=%d\n",p1);
			if(A[ed1]==st){
				fr1=R[fr1];
				ed1=R[ed1];
				Add(p1);
				continue;
			}
//			puts("@");
			while(A[fr2]!=st){
				fr2=R[fr2];
				ed2=R[ed2];
				Add(p2);
			}
			int len=1;
			while(A[R[ed1]]!=st&&A[R[fr2]]==st){
				fr1=R[fr1];
				ed1=R[ed1];
				Add(p1);
				
				fr2=R[fr2];
				ed2=R[ed2];
				Add(p2);
				len++;
			}
			fr2=R[fr2];
			ed2=R[ed2];
			Add(p2);
			Addans(p1,p2);
			Swap(fr1,ed1,fr2,ed2);
			int tmp_len=len;
			while(len--){
				fr2=L[fr2];
				ed2=L[ed2];
				Del(p2);
			}
			Addans(p1,p2);
			Swap(fr1,ed1,fr2,ed2);
			i+=tmp_len-1;
		}
	}
	printf("%d\n",sz);
	for(int i=1;i<=sz;i++)printf("%d %d\n",Ans[i][0],Ans[i][1]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5708kb

input:

6 3
BCCBCC
BBCBBC

output:

2
2 1
4 3

result:

ok moves = 2

Test #2:

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

input:

2 1
BC
BC

output:

1
2 1

result:

ok moves = 1

Test #3:

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

input:

2 1
BB
CC

output:

0

result:

ok moves = 0

Test #4:

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

input:

2 1
CC
BB

output:

0

result:

ok moves = 0

Test #5:

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

input:

3 1
CCC
BBB

output:

0

result:

ok moves = 0

Test #6:

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

input:

3 1
CBC
BCB

output:

1
2 2

result:

ok moves = 1

Test #7:

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

input:

3 2
BBB
CCC

output:

0

result:

ok moves = 0

Test #8:

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

input:

3 2
BCB
BCC

output:

1
3 2

result:

ok moves = 1

Test #9:

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

input:

4 2
CCCB
BBCB

output:

2
3 4
3 3

result:

ok moves = 2

Test #10:

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

input:

9 6
CCCBCCCBB
BBBCBBBCC

output:

3
7 7
8 8
1 1

result:

ok moves = 3

Test #11:

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

input:

21 3
CCCCBBCBCCCCCCCBCCCCC
BBCCBCBBBBBBBBBCBBBBB

output:

12
5 3
7 5
9 7
10 8
12 10
13 11
15 13
16 15
18 17
19 18
19 21
19 20

result:

ok moves = 12

Test #12:

score: -100
Wrong Answer
time: 0ms
memory: 5732kb

input:

49 41
CBCCBCCBCCBCCBCCCBBCCBCBBCCCBBCCBCBCBCBCCCCBCBCCB
BCCCCBCBBBBCBCBBBBBCBBBBCCCCBCBBCBBCBBBBCBCBCBBBC

output:

31
10 10
14 11
16 12
17 13
19 15
20 16
21 17
23 19
26 20
27 22
28 23
29 25
30 27
31 29
32 31
33 32
34 33
37 36
40 38
42 40
43 41
44 44
45 45
47 48
49 49
44 2
44 1
46 3
46 2
1 7
1 4

result:

wrong answer The final sequences are not correct!