QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#142221 | #6683. Difficult Constructive Problem | Ayaya | AC ✓ | 19ms | 4048kb | C++14 | 2.2kb | 2023-08-18 18:10:22 | 2023-08-18 18:10:25 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<unordered_map>
#include<vector>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define int long long
#define lc (x<<1)
#define rc (x<<1|1)
#define pii pair<int,int>
#define mkp make_pair
#define fi first
#define se second
#define min(x,y) (x>y?y:x)
#define max(x,y) (x<y?y:x)
const int Mx=500005,p=998244353;
int read(){
char ch=getchar();
int Alice=0,Aya=1;
while(ch<'0'||ch>'9'){
if(ch=='-') Aya=-Aya;
ch=getchar();
}
while(ch>='0'&&ch<='9')
Alice=(Alice<<3)+(Alice<<1)+(ch^48),ch=getchar();
return (Aya==1?Alice:-Alice);
}
int n,k;
string s;
int Calc(string t){
char lst=t[0];
int res=0;
for(char c:t){
if(c!=lst) res++;
lst=c;
}
return res;
}
priority_queue<string,vector<string>,greater<string> >q;
void Solve(string t){
string r=t;
for(char &c:r){
if(c=='?') c='0';
}
int ans=Calc(r);
//cout<<r<<" "<<ans<<endl;
int cnt=k-ans;
if(cnt%2!=0) return;
cnt/=2;
if(cnt<0){
cnt=-cnt;
for(int i=n-2;i>=1;i--){
if(cnt&&'0'==r[i-1]&&'1'==r[i+1]&&t[i]=='?'){
r[i]='1';
}
if(cnt&&r[i]!=r[i-1]&&r[i]!=r[i+1]&&t[i]=='?'){
r[i]='1';
cnt--;
}
}
}
else if(cnt>0){
for(int i=n-2;i>=1;i--){
if(cnt&&r[i]==r[i-1]&&r[i]==r[i+1]&&t[i]=='?'){
r[i]='1';
cnt--;
}
}
}
if(cnt==0){
for(int i=1;i+1<n;i++){
if(r[i]=='1'&&r[i-1]!=r[i+1]&&t[i]=='?'){
r[i]='0';
}
}
q.push(r);
}
}
signed main(){
int T=read();
while(T--){
n=read(),k=read();
cin>>s;
if(s[0]=='?'){
string t=s;
for(char c='0';c<='1';c++){
t[0]=c;
if(t[n-1]=='?'){
for(char d='0';d<='1';d++){
t[n-1]=d;
Solve(t);t[n-1]='?';
}
}
else{
Solve(t);
}
}
}
else if(s[0]!='?'&&s[n-1]=='?'){
string t=s;
for(char c='0';c<='1';c++){
t[n-1]=c;
Solve(t);t[n-1]='?';
}
}
else Solve(s);
if(!q.empty()){
cout<<q.top()<<endl;
}
else{
cout<<"Impossible"<<endl;
}
while(!q.empty()) q.pop();
}
return 0;
}
/*
Hello!!
Sample:
-------------------
*/
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3504kb
input:
5 9 6 1?010??01 9 5 1?010??01 9 6 100101101 9 5 100101101 9 3 ????????1
output:
100100101 Impossible 100101101 Impossible 000000101
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 19ms
memory: 4048kb
input:
6110 2 0 01 9 5 ????110?? 18 8 ???111???00??010?? 11 8 001??01???0 2 0 00 8 1 ?????111 11 2 110???01??? 13 7 ??100???01??1 6 2 ?????0 18 8 110??11??011??110? 12 5 00??011??00? 20 10 ??01???0011???0010?? 1 0 ? 13 6 ???10??110??0 6 2 00???1 17 10 ??010??001???11?? 5 2 ????0 14 7 010???00???11? 2 1 01 ...
output:
Impossible 001011001 000111000000101010 00101010010 00 00000111 11000001111 0010000101001 000010 110001100011001101 000001101001 00010000011001001010 0 0001000110010 Impossible 00010010010101100 00010 01000100010111 01 0100100001 Impossible 001100010100100101 00111111000 000 01111001 001000000100010...
result:
ok 6110 lines