QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#269571#7743. Grand FinaleGeospizaWA 93ms54784kbC++141.6kb2023-11-29 19:37:422023-11-29 19:37:42

Judging History

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

  • [2023-11-29 19:37:42]
  • 评测
  • 测评结果:WA
  • 用时:93ms
  • 内存:54784kb
  • [2023-11-29 19:37:42]
  • 提交

answer

#include <bits/stdc++.h>
#define ll int
#define Ma 5005
#define N 10005
#define pb push_back
using namespace std;
ll dp[Ma][Ma];
ll n,m;
ll inf=1e9;
string s,t;

bool check(ll lim,ll cnt1,ll cnt2)
{
	ll len=n;
	ll id=0;
	while (len<lim)
	{
		if (cnt1==0&&cnt2==0)
			return 0;
		if (cnt2)
		{
			if (id+2>=m)
				return 1;
			cnt2--;
			cnt1+=(t[id]=='Q'),cnt2+=(t[id]=='B');
			id++;
			cnt1+=(t[id]=='Q'),cnt2+=(t[id]=='B');
			id++;
			lim++;
		}
		else if (cnt1)
		{
			if (id+1>=m)
				return 1;
			cnt1--;
			cnt1+=(t[id]== 'Q'),cnt2+=(t[id]=='B');
			id++;
		}
	}
	return dp[id][cnt2]<=cnt1;
}


void sol()
{
	cin>>n>>m;
	cin>>s>>t;
	for (ll i=0;i<=m+n+1;i++)
		for (ll j=0;j<=m+n+1;j++)
			dp[i][j]=inf;
	for (ll i=0;i<=n+m;i++)
		dp[m][i]=0,dp[m-1][i]=(i==0&&t[m-1]!='Q');
	for (ll i=m-1;i>=0;i--)
	{
		for (ll j=0;j<=n+m;j++)
		{
			ll l=0,r=0;//l- +1 r- +2
			if (t[i]=='B')
				r++;
			else if (t[i]=='Q')
				l++;
			
			dp[i][j]=max(1,min({dp[i][j],dp[i+1][j+r]+1-l}));
			if (j)
				dp[i][j]=min({dp[i][j],dp[i+2][j+r-1]-l});
			dp[i][j]=max(dp[i][j],0);
			//printf("i=%d j=%d w=%d\n",i,j,dp[i][j]);	
		}
	}
	ll cnt1=0,cnt2=0;
	for (ll i=0;i<n;i++)
		cnt1+=(s[i]=='Q'),cnt2+=(s[i]=='B');
	ll l=n,r=n+m;
	ll ans=inf;
	while (l<=r)
	{
		ll mid=(l+r)>>1;
		if (check(mid,cnt1,cnt2))
			ans=min(ans,mid),r=mid-1;
		else
			l=mid+1;
	}
	if (ans==inf)
		printf("IMPOSSIBLE\n");
	else
		printf("%d\n",ans);
	return ;
}

int main()
{
	ios::sync_with_stdio(0);cin.tie(0);
	ll tt=1;
	cin>>tt;
	while (tt--)
		sol();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 93ms
memory: 54784kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

184
67
165
161
118
118
IMPOSSIBLE
126
135
90
33
160
74

result:

wrong answer 2nd lines differ - expected: '372', found: '67'