QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#487643 | #7743. Grand Finale | CSQ | WA | 1ms | 5632kb | C++14 | 3.6kb | 2024-07-23 04:14:52 | 2024-07-23 04:14:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int dp[2511][2511]; //is it possible to draw all cards 1 to i st j draw 1 cards used
int dp2[2511][2511]; //min draw 2 cards needed to win from i if you start from j draw 1 cards
int main()
{
owo
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
string s,t;
cin>>s>>t;
for(int i=0;i<=m+1;i++){
for(int j=0;j<=m+1;j++)dp[i][j] = 0;
}
dp[0][0] = 1;
int cnt1 = 0,cnt2 = 0;
for(char c:s){
cnt1 += c == 'Q';
cnt2 += c == 'B';
}
for(int i=0;i<m;i++){
for(int j=0;j<=i;j++){
if(!dp[i][j])continue;
int k = (i-j)/2; //draw 2 used
assert(cnt1 >= j);
assert(cnt2 >= k);
if(cnt1 - j > 0)dp[i+1][j+1] = 1;
if(cnt2 - k > 0 && i+2<=m)dp[i+2][j] = 1;
}
cnt1 += t[i] == 'Q';
cnt2 += t[i] == 'B';
}
for(int i=0;i<=m+5;i++){
for(int j=0;j<=m+5;j++)dp2[i][j] = 1e9;
}
for(int j=0;j<=m+1;j++)dp2[m][j] = dp2[m+1][j] = 0;
for(int i=m-1;i>=0;i--){
for(int j=0;j<=m;j++){
if(j){
if(t[i] == 'W')dp2[i][j] = min(dp2[i][j],dp2[i+2][j-1]);
if(t[i] == 'Q')dp2[i][j] = min(dp2[i][j],max(0,dp2[i+1][j-1]-1));
if(t[i] == 'B')dp2[i][j] = min(dp2[i][j],dp2[i+1][j]);
}
//use draw 1
if(t[i] == 'W')dp2[i][j] = min(dp2[i][j],dp2[i+1][j] + 1);
if(t[i] == 'Q')dp2[i][j] = min(dp2[i][j],dp2[i+1][j]);
if(t[i] == 'B')dp2[i][j] = min(dp2[i][j],dp2[i+1][j+1] + 1);
}
}
// for(int i=0;i<m;i++){
// for(int j=0;j<=m-i;j++){
// if(dp2[i][j] > m)cout<<"INF ";
// else cout<<dp2[i][j]<<" ";
// }
// cout<<'\n';
// }
///
cnt1 = cnt2 = 0;
for(char c:s){
cnt1 += c == 'Q';
cnt2 += c == 'B';
}
int ans = 1e9;
for(int i=0;i<=m;i++){
for(int j=0;j<=i;j++){
if(!dp[i][j])continue;
int k = (i-j)/2;
int hand = sz(s) + k;
//cout<<cnt1<<" "<<cnt2<<'\n';
//cout<<i<<" "<<j<<" "<<k<<" "<<cnt1-j<<" "<<cnt2-k<<'\n';
int two = min(m,cnt2-k);
if(cnt1 - j >= dp2[i][two]){
ans = min(ans,hand);
}
}
if(i<m){
cnt1 += t[i] == 'Q';
cnt2 += t[i] == 'B';
}
}
if(ans==1e9){
cout<<"IMPOSSIBLE"<<'\n';
}
else cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5632kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 5560kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
4 4
result:
wrong answer 1st lines differ - expected: '3', found: '4'