QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#241990 | #7743. Grand Finale | triccsr | WA | 37ms | 37148kb | C++20 | 3.2kb | 2023-11-06 20:54:38 | 2023-11-06 20:54:38 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<assert.h>
#include<list>
#include <string>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define mid (x+y>>1)
#define lowbit(x) (x&(-x))
#define sqr(x) (1ll*(x)*(x))
#define NM 2505
#define nm 1000005
using namespace std;
const double pi=acos(-1);
const ll inf=1e9+7;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m;
int d[NM][NM*2];
int tot;
char a[NM],b[NM];
int solve(){
m=read();n=read();
scanf("%s",b+1);
scanf("%s",a+1);
tot=0;
inc(i,1,n)if(a[i]=='B')tot++;
inc(i,1,m)if(b[i]=='B')tot++;
inc(j,0,tot)d[n][j]=d[n+1][j]=j;
dec(i,n-1,0){
inc(j,0,tot){
d[i][j]=inf;
if(j>0){
if(a[i+1]=='Q'){
d[i][j]=min(d[i][j],d[i+2][j-1]);
}
if(a[i+1]=='B'){
d[i][j]=min(d[i][j],d[i+2][j]);
}
if(a[i+1]=='W'){
d[i][j]=min(d[i][j],d[i+2][j-1]+1);
}
}
{
int t=inf;
if(a[i+1]=='Q'){
t=min(t,d[i+1][j]);
}
if(a[i+1]=='B'&&j<=tot){
t=min(t,d[i+1][j+1]);
}
if(a[i+1]=='W'){
t=min(t,d[i+1][j]+1);
}
t=max(t,1);
d[i][j]=min(d[i][j],t);
}
d[i][j]=max(d[i][j],j);
// printf("%d ",d[i][j]);
}
// putchar('\n');
}
int init_b=0,init_q=0,init_w=0;
inc(i,1,m){
if(b[i]=='B')init_b++;
if(b[i]=='Q')init_q++;
if(b[i]=='W')init_w++;
}
// puts("--------");
inc(k,m,n+m){
int num_b=init_b,num_q=init_q,num_w=init_w;
inc(i,0,n){
if(i>0){
if(a[i]=='B')num_b++;
if(a[i]=='Q')num_q++;
if(a[i]=='W')num_w++;
}
if(num_b*2+num_q<i+m)break;
if(num_b>=k-m){
int actual_b=num_b-(k-m);
int actual_q=k-2-num_w-actual_b;
if(actual_b>=0&&actual_q>=0&&actual_q<=num_q){
inc(j,i+1,i+2)if(j<=n){
if(a[j]=='B')actual_b++;
if(a[j]=='Q')actual_q++;
}
// printf("%d %d %d %d\n",k,i,actual_b,actual_q);
if(d[i+2][actual_b]<=actual_b+actual_q){
printf("%d\n",k);
return 0;
}
}
}
}
}
printf("IMPOSSIBLE\n");
return 0;
}
int main(){
int _=read();while(_--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 37ms
memory: 37148kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
IMPOSSIBLE IMPOSSIBLE 313 IMPOSSIBLE 118 IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE 33 IMPOSSIBLE IMPOSSIBLE
result:
wrong answer 1st lines differ - expected: '184', found: 'IMPOSSIBLE'