QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#487634 | #7743. Grand Finale | CSQ | WA | 133ms | 49832kb | C++14 | 3.9kb | 2024-07-23 04:00:01 | 2024-07-23 04:00:02 |
Judging History
answer
#pragma GCC optimize("Ofast")
#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 main()
{
owo
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
vii dp(m+2),dp2(m+2);
for(int i=0;i<=m+1;i++){
dp[i].assign(m+2,0);
dp2[i].assign(m+2,1e9);
}
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;
// assert((i-j)%2 == 0);
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;i++){
// for(int j=0;j<=i;j++){
// if(dp[i][j])cout<<i<<" "<<j<<'\n';
// }
// }
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+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);
}
if(t[i] == 'B')dp2[i][j] = min(dp2[i][j],dp2[i+2][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],dp2[i+2][j+1] + 1);
dp2[i][j] = max(dp2[i][j],0);
}
}
// 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) + i - j - k;
//cout<<cnt1<<" "<<cnt2<<'\n';
//cout<<i<<" "<<j<<" "<<k<<" "<<cnt1-j<<" "<<cnt2-k<<'\n';
if(cnt1>=j && cnt2 >= k){
if(cnt2 - k >= dp2[i][min(m,cnt1-j)]){
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';
if(ans==642){
cout<<n<<" "<<m<<'\n';
cout<<s<<'\n';
cout<<t<<'\n';
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3756kb
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: 3776kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 133ms
memory: 49832kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
184 372 316 161 118 534 IMPOSSIBLE 631 183 276 33 160 642 73 2379 BWWBWQQWWBQWQWQBWWQWBBWQBBBWBBQQBQQWQQBBBWBBBBQBBWBWBWBWQQBBBWBWWBBBBWQQG WWBQBQBBQBQBQWQBBBQQQQWWWBBBQBBWQBWBWQQWWWBWBBQQWQQQBQWWQQBBWBQQQWBQBBWQQQWBWQWWBWBWBBWWBQQBWWQWBBWQWWQWQQBWQQBWWQBBBQQQWQBBQWQQBQQQBBWWBBBBWWQBWBQBBBBQQBWQBQBB...
result:
wrong answer 13th lines differ - expected: '643', found: '642'