QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#378043#8523. Puzzle IIDays_of_Future_Past#WA 1ms5928kbC++233.6kb2024-04-05 23:11:482024-04-05 23:11:49

Judging History

This is the latest submission verdict.

  • [2024-04-05 23:11:49]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 5928kb
  • [2024-04-05 23:11:48]
  • Submitted

answer

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=1;i<=n;++i)
#define pb push_back
#define mp make_pair
#define N 310000
#define inf 1000000007
using namespace std;
struct node
{
	int l,r; //son
	int cnt; //size
	int key; //randomkey
    int sum[2]; //count
	int num; //0 or 1
}a[2*N];
int root;
int n,k,root1,root2;
char s1[N],s2[N];
void update(int x)
{
	int l=a[x].l,r=a[x].r;
	a[x].cnt=a[l].cnt+a[r].cnt+1;
    a[x].sum[0]=a[l].sum[0]+a[r].sum[0]; 
    a[x].sum[1]=a[l].sum[1]+a[r].sum[1];
    a[x].sum[a[x].num]++;
}
int merge(int x,int y)
{
	if (x==0||y==0)return x+y;
	if (a[x].key>a[y].key)
	{
		a[x].r=merge(a[x].r,y);
		update(x);
		return x;
	}
	else
	{
		a[y].l=merge(x,a[y].l);
		update(y);
		return y;
	}
}
void split(int t,int k,int &x,int &y)
{
	if (t==0){x=0; y=0; return;}
	if (a[a[t].l].cnt+1<=k)
	{
		x=t;
		split(a[t].r,k-a[a[t].l].cnt-1,a[t].r,y);
	}
	else 
	{
		y=t;
		split(a[t].l,k,x,a[t].l);
	}
	if (x!=0)update(x);
	if (y!=0)update(y);
}
int find(int t,int x)
{
    if (a[t].sum[x]==0)return -1;
    if (a[t].num==x)return a[a[t].l].cnt+1;
    if (a[a[t].l].sum[x]!=0)return find(a[t].l,x); 
    else return a[a[t].l].cnt+1+find(a[t].r,x);
}
int tot=0;
int newnode(char c)
{
    tot++;
    a[tot].l=a[tot].r=0;
    a[tot].cnt=1;
    a[tot].key=rand()*rand();
    a[tot].sum[0]=a[tot].sum[1]=0;
    if (c=='B')a[tot].num=0; else a[tot].num=1;
    a[tot].sum[a[tot].num]++;
    return tot;
}
void print(int t)
{
    if (t==0)return;
    print(a[t].l);
    if (a[t].num==0)putchar('B'); else putchar('C');
    print(a[t].r);
}
void work(int x,int y)
{
    printf("%d %d\n",x,y);
    int p1,p2,p3;
    if (x+k-1<=n){ split(root1,x-1,p1,p2); split(p2,k,p2,p3); }
    if (x+k-1>n){ int l=k-(n-x+1); split(root1,l,p3,root1); root1=merge(root1,p3); split(root1,n-k+1,p1,p2); p3=0; }
    // print(p1); puts("");
    // print(p2); puts("");
    // print(p3); puts("");
    int q1,q2,q3;
    if (y+k-1<=n){ split(root2,y-1,q1,q2); split(q2,k,q2,q3); }
    if (y+k-1>n){ int l=k-(n-y+1); split(root2,l,q3,root2); root2=merge(root2,q3); split(root2,n-k,q1,q2); q3=0; }
    // print(q1); puts("");
    // print(q2); puts("");
    // print(q3); puts("");
    root1=merge(merge(p1,q2),p3);
    root2=merge(merge(q1,p2),q3);
    if (x+k-1>n){ int l=k-(n-x+1); split(root1,n-l,p1,p2); root1=merge(p2,p1); }
    if (y+k-1>n){ int l=k-(n-y+1); split(root2,n-l,q1,q2); root2=merge(q2,q1); }
}
int main()
{
    //freopen("1.in","r",stdin);
	srand(19260817);

    cin>>n>>k;
    scanf("%s%s",s1+1,s2+1);

    //always swap B from s1 to s2
    int b=0,c=0;
    rep(i,n)if (s1[i]=='B')b++; else c++;
    if (b>c)
    {
        rep(i,n)if (s1[i]=='B')s1[i]='C'; else s1[i]='B';
        rep(i,n)if (s2[i]=='B')s2[i]='C'; else s2[i]='B';
    } 
    cout<<2*min(b,c)<<endl;
    //init treap 
    root1=0,root2=0;
    rep(i,n)root1=merge(root1,newnode(s1[i]));
    rep(i,n)root2=merge(root2,newnode(s2[i]));

    while (1)
    {
        //find any B and C
        int x=find(root1,0);
        int y=find(root2,1);
        //cout<<x<<" "<<y<<endl;
        if (x==-1 || y==-1)break;        
        //update treap
        //print(root1); puts("");
        //print(root2); puts("");
        if (y>k)work(x,y-k); else work(x,n-(k-y));
        //print(root1); puts("");
        //print(root2); puts("");
        if (y>=k)work(x,y-k+1); else work(x,n-(k-y)+1);
        //print(root1); puts("");
        //print(root2); puts("");
    }
	return 0;
}	

詳細信息

Test #1:

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

input:

6 3
BCCBCC
BBCBBC

output:

4
1 3
1 4
4 1
4 2

result:

ok moves = 4

Test #2:

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

input:

2 1
BC
BC

output:

2
1 1
1 2

result:

ok moves = 2

Test #3:

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

input:

2 1
BB
CC

output:

0

result:

ok moves = 0

Test #4:

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

input:

2 1
CC
BB

output:

0

result:

ok moves = 0

Test #5:

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

input:

3 1
CCC
BBB

output:

0

result:

ok moves = 0

Test #6:

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

input:

3 1
CBC
BCB

output:

2
2 1
2 2

result:

ok moves = 2

Test #7:

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

input:

3 2
BBB
CCC

output:

0

result:

ok moves = 0

Test #8:

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

input:

3 2
BCB
BCC

output:

2
2 2
2 3

result:

ok moves = 2

Test #9:

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

input:

4 2
CCCB
BBCB

output:

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

result:

wrong output format Extra information in the output file