QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592994 | #7736. Red Black Tree | daduoli# | RE | 0ms | 0kb | C++14 | 1.6kb | 2024-09-27 10:59:54 | 2024-09-27 10:59:54 |
answer
#include<bits/stdc++.h>
#define Yzl unsigned long long
#define mp make_pair
#define pi pair<LL,LL>
#define fi first
#define se second
#define pb emplace_back
typedef long long LL;
using namespace std;
const Yzl Lty=20120712;
const int MAXN=3e3+10;
int n,m,V,s1,s2,s3;
pi f[MAXN][MAXN];
char s[MAXN],ch[MAXN];
bool chk(int x) {
for(int i=0;i<=m;++i) for(int j=0;j<=V;++j) f[i][j].fi=-Lty;
if(2*s2+s1>=m) return true;
f[0][s2]=mp(s1,-s3);
for(int i=0;i<=m;++i) {
for(int j=0;j<=V;++j) {
if(f[i][j].fi<0) continue;
// if(x==2) {
// cout<<i<<" "<<j<<" "<<f[i][j].fi<<endl;
// }
if(2*j+f[i][j].fi>=m-i) return true;
if(j&&i+2<=m) {
int t1=0,t2=0,t3=0;
if(ch[i+1]=='Q') ++t1;
if(ch[i+1]=='B') ++t2;
if(ch[i+1]=='W') ++t3;
if(-f[i][j].se+j+f[i][j].fi+1<=x) {
if(ch[i+2]=='Q') ++t1;
if(ch[i+2]=='B') ++t2;
if(ch[i+2]=='W') ++t3;
}
f[i+2][j+t2-1]=max(f[i+2][j+t2-1],mp(f[i][j].fi+t1,f[i][j].se-t3));
}
if(f[i][j].fi) {
int t1=0,t2=0,t3=0;
if(ch[i+1]=='Q') ++t1;
if(ch[i+1]=='B') ++t2;
if(ch[i+1]=='W') ++t3;
f[i+1][j+t2]=max(f[i+1][j+t2],mp(f[i][j].fi+t1-1,f[i][j].se-t3));
}
}
} return false;
}
void vmain() {
scanf("%d%d%s%s",&n,&m,s+1,ch+1); V=m/2;
s1=0,s2=0,s3=0;
for(int i=1;i<=n;++i) {
if(s[i]=='Q') ++s1;
if(s[i]=='B') ++s2;
if(s[i]=='W'||s[i]=='G') ++s3;
}
int l=n-1,r=n+m+1,mid;
while(l+1<r) { mid=(l+r)/2;
if(chk(mid)) r=mid;
else l=mid;
} if(r!=n+m+1) printf("%d\n",r); else puts("IMPOSSIBLE");
}
int main () {
int T=1; scanf("%d",&T); while(T--) vmain();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3