QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#713686 | #8523. Puzzle II | Loging | WA | 2ms | 7788kb | C++14 | 2.2kb | 2024-11-05 20:17:27 | 2024-11-05 20:17:27 |
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;
}
bool flag;
int Inv;
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;
if(!Inv){Ans[sz][0]=p1;Ans[sz][1]=p2;}
else{Ans[sz][0]=p2;Ans[sz][1]=p1;}
if(flag)Inv=!Inv;
}
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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5636kb
input:
6 3 BCCBCC BBCBBC
output:
2 2 1 4 3
result:
ok moves = 2
Test #2:
score: 0
Accepted
time: 1ms
memory: 5640kb
input:
2 1 BC BC
output:
1 2 1
result:
ok moves = 1
Test #3:
score: 0
Accepted
time: 1ms
memory: 7676kb
input:
2 1 BB CC
output:
0
result:
ok moves = 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 7692kb
input:
2 1 CC BB
output:
0
result:
ok moves = 0
Test #5:
score: 0
Accepted
time: 0ms
memory: 7680kb
input:
3 1 CCC BBB
output:
0
result:
ok moves = 0
Test #6:
score: 0
Accepted
time: 2ms
memory: 7760kb
input:
3 1 CBC BCB
output:
1 2 2
result:
ok moves = 1
Test #7:
score: 0
Accepted
time: 0ms
memory: 7768kb
input:
3 2 BBB CCC
output:
0
result:
ok moves = 0
Test #8:
score: 0
Accepted
time: 1ms
memory: 7788kb
input:
3 2 BCB BCC
output:
1 3 2
result:
ok moves = 1
Test #9:
score: 0
Accepted
time: 1ms
memory: 7684kb
input:
4 2 CCCB BBCB
output:
2 3 4 3 3
result:
ok moves = 2
Test #10:
score: 0
Accepted
time: 1ms
memory: 5640kb
input:
9 6 CCCBCCCBB BBBCBBBCC
output:
3 7 7 8 8 1 1
result:
ok moves = 3
Test #11:
score: 0
Accepted
time: 1ms
memory: 5732kb
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: 5644kb
input:
49 41 CBCCBCCBCCBCCBCCCBBCCBCBBCCCBBCCBCBCBCBCCCCBCBCCB BCCCCBCBBBBCBCBBBBBCBBBBCCCCBCBBCBBCBBBBCBCBCBBBC
output:
31 10 10 11 14 16 12 13 17 19 15 16 20 21 17 19 23 26 20 22 27 28 23 25 29 30 27 29 31 32 31 32 33 34 33 36 37 40 38 40 42 43 41 44 44 45 45 48 47 49 49 2 44 44 1 3 46 46 2 7 1 1 4
result:
wrong answer The final sequences are not correct!