QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#243220#7743. Grand Finaleqkm66666WA 97ms29468kbC++174.3kb2023-11-07 22:19:582023-11-07 22:19:59

Judging History

你现在查看的是最新测评结果

  • [2023-11-07 22:19:59]
  • 评测
  • 测评结果:WA
  • 用时:97ms
  • 内存:29468kb
  • [2023-11-07 22:19:58]
  • 提交

answer

#include<iostream>
#include<time.h>
using namespace std;
const int maxn=2600;
int T,n,m;
int dp[maxn][maxn];
int cur,cnt[2];
int Ans;
string H,P;

bool dfs(int cur,int LIM,int c1,int c2){
    if(cur>=m) return true;
    bool flag=false;
    if(c1){
        int cc1=c1-1,cc2=c2;
        if(P[cur]=='B') cc2++;
        if(P[cur]=='Q') cc1++;
        if(dfs(cur+1,LIM,cc1,cc2)){
            flag=true;
        }
    }
    if(c2){
        int cc1=c1,cc2=c2-1;
        if(P[cur]=='B') cc2++;
        if(P[cur]=='Q') cc1++;
        if(LIM>=1){
            if(P[cur+1]=='B') cc2++;
            if(P[cur+1]=='Q') cc1++;
            if(dfs(cur+2,LIM-1,cc1,cc2)){
                flag=true;
            }
        }
        else{
            if(cur+1>=m||P[cur+1]!='G'){
                if(dfs(cur+2,LIM,cc1,cc2)){
                    flag=true;
                }
            }
        }
    }
    return flag;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    cin>>T;
    while(T--){
        // int lim=20;
        // n=1+rand()%lim;
        // m=1+rand()%lim;
        // H.resize(n);
        // P.resize(m);
        // for(int i=0;i<n;i++){
        //     switch(rand()%3){
        //         case 0:H[i]='B';break;
        //         case 1:H[i]='Q';break;
        //         case 2:H[i]='W';break;
        //     }
        // }
        // for(int i=0;i<m;i++){
        //     switch(rand()%3){
        //         case 0:P[i]='B';break;
        //         case 1:P[i]='Q';break;
        //         case 2:P[i]='W';break;
        //     }
        // }
        // if(rand()%2){
        //     H[rand()%n]='G';
        // }
        // else{
        //     P[rand()%m]='G';
        // }
        cin>>n>>m;
        cin>>H;
        cin>>P;
        for(int i=0;i<=m;i++){
            for(int j=0;j<=m+n;j++){
                dp[i][j]=m+1;
            }
        }
        for(int i=0;i<=m+n;i++){
            dp[m][i]=0;
            dp[m-1][i+1]=0;
        }
        for(int i=m;i>0;i--){
            for(int j=m+n;j>=0;j--){
                if(i>=2&&P[i-1]!='G'){
                    if(P[i-2]=='B'&&j>=1){
                        dp[i-2][j]=min(dp[i-2][j],dp[i][j]);
                    }
                    if(P[i-2]=='Q'){
                        dp[i-2][j+1]=min(dp[i-2][j+1],max(dp[i][j]-1,0));
                    }
                    dp[i-2][j+1]=min(dp[i-2][j+1],dp[i][j]);
                }
                if(i>=1){
                    if(P[i-1]=='B'&&j>=1){
                        dp[i-1][j-1]=min(dp[i-1][j-1],dp[i][j]+1);
                    }
                    if(P[i-1]=='Q'){
                        dp[i-1][j]=min(dp[i-1][j],max(dp[i][j],1));
                    }
                    dp[i-1][j]=min(dp[i-1][j],dp[i][j]+1);
                }
            }
        }
        Ans=n+m+1;
        cnt[0]=0;
        cnt[1]=0;
        cur=n;
        for(int i=0;i<n;i++){
            if(H[i]=='B'){
                cnt[0]++;
            }
            if(H[i]=='Q'){
                cnt[1]++;
            }
        }
        for(int i=0;;){
            if(dp[i][cnt[0]]<=cnt[1]){
                Ans=cur;
                break;
            }
            if(i>=m||(cnt[0]==0&&cnt[1]==0)){
                break;
            }
            if(cnt[0]){
                cnt[0]--;
                if(P[i]=='B'){
                    cnt[0]++;
                }
                if(P[i]=='Q'){
                    cnt[1]++;
                }
                cur++;
                i++;
                if(i<m){
                    if(P[i]=='B'){
                        cnt[0]++;
                    }
                    if(P[i]=='Q'){
                        cnt[1]++;
                    }
                    i++;
                }
            }
            else{
                cnt[1]--;
                if(P[i]=='B'){
                    cnt[0]++;
                }
                if(P[i]=='Q'){
                    cnt[1]++;
                }
                i++;
            }
        }

        if(Ans==n+m+1){
            puts("IMPOSSIBLE");
        }
        else{
            printf("%d\n",Ans);
        }
    }
    //system("pause");
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3912kb

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: 3932kb

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 97ms
memory: 29468kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

184
372
164
161
118
534
IMPOSSIBLE
631
183
276
33
160
643

result:

wrong answer 3rd lines differ - expected: '316', found: '164'