QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#329546#7743. Grand Finale275307894aTL 2ms4168kbC++141.9kb2024-02-16 21:02:162024-02-16 21:02:16

Judging History

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

  • [2024-02-16 21:02:16]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:4168kb
  • [2024-02-16 21:02:16]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=2500+5,M=N*4+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
int n,m;char s[N];
int dp[N][N*2];
void Solve(){
	int i,j;scanf("%d%d",&n,&m);
	scanf("%s",s+1);int c1=count(s+1,s+n+1,'Q'),c2=count(s+1,s+n+1,'B'),c3=n-c1-c2-1;
	scanf("%s",s+1);int cnt=c2+count(s+1,s+m+1,'B');
	Me(dp[m+1],0x3f);for(int i=0;i<=cnt;i++) dp[m+1][i]=0;
	for(int i=m;i;i--){
		Me(dp[i],0x3f);
		for(int j=0;j<=cnt;j++){
			int w1=0,w2=0;
			if(s[i]=='Q') w1++;if(s[i]=='B') w2++;
			dp[i][j]=max(1,dp[i+1][j+w2]-w1+1);
			if(j){
				dp[i][j]=min(dp[i][j],max(dp[min(i+2,m+1)][j-1+w2]-w1,0));
			}gdb(i,j,dp[i][j]);
		}
	}
	int tot=n,flag=0;
	for(i=0;i<=m;i++){
		if(i){
			if(c2){
				tot++;c2--;
				if(s[i]=='Q') c1++;if(s[i]=='B') c2++;
				i++;
				if(s[i]=='Q') c1++;if(s[i]=='B') c2++;
			}else if(c1){
				c1--;
				if(s[i]=='Q') c1++;if(s[i]=='B') c2++;
			}else break;
		}
		gdb(i);
		if(dp[i+1][c2]<=c1) {flag=1;printf("%d\n",tot);break;}
	}
	if(!flag) puts("IMPOSSIBLE");
}
int main(){
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 4096kb

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Time Limit Exceeded

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:


result: