QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#713625 | #8523. Puzzle II | Loging | WA | 1ms | 7800kb | C++14 | 2.0kb | 2024-11-05 20:05:22 | 2024-11-05 20:05:23 |
Judging History
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;
}
void Addans(int p1,int p2){
sz++;
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)m=n-m;
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: 7752kb
input:
6 3 BCCBCC BBCBBC
output:
2 2 1 4 3
result:
ok moves = 2
Test #2:
score: 0
Accepted
time: 1ms
memory: 5740kb
input:
2 1 BC BC
output:
1 2 1
result:
ok moves = 1
Test #3:
score: 0
Accepted
time: 1ms
memory: 7756kb
input:
2 1 BB CC
output:
0
result:
ok moves = 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 7752kb
input:
2 1 CC BB
output:
0
result:
ok moves = 0
Test #5:
score: 0
Accepted
time: 0ms
memory: 7800kb
input:
3 1 CCC BBB
output:
0
result:
ok moves = 0
Test #6:
score: 0
Accepted
time: 1ms
memory: 7676kb
input:
3 1 CBC BCB
output:
1 2 2
result:
ok moves = 1
Test #7:
score: 0
Accepted
time: 1ms
memory: 5632kb
input:
3 2 BBB CCC
output:
0
result:
ok moves = 0
Test #8:
score: -100
Wrong Answer
time: 1ms
memory: 5732kb
input:
3 2 BCB BCC
output:
1 2 1
result:
wrong answer The final sequences are not correct!