题解 | #恋恋的01串大冒险#
恋恋的01串大冒险
https://ac.nowcoder.com/acm/problem/309216
把0看做输了一把游戏,1看做赢了一把游戏,如果一个人连输k把就不想玩,然而我们有任意次机会可以让一个输了的游戏变成赢得,怎么变才能让他玩的把数最多。
自然是等他输到第k把的时候就马上给他改成赢的。
鉴于之前评论区有神人认为我没有及时,详细地,像保姆一样地解答他的疑惑,故评论区不开放,有疑问的小伙伴可以私信我
#include<iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N=2e5+7;
int ans[N];
int score=0;
int cur=0;
string s;
int main(){
int n,k;
cin>>n>>k;
cin>>s;
for(int i=1;i<=n;i++){
int x=s[i-1]-'0';
if(x)score=0;
else{
score--;
if(score==-k){ //在这天死亡
ans[cur]=i-1;//第i天死亡,存活了i-1天
cur++;
score=0;
}
}
}
if(cur<=n){
for(int i=cur;i<=n;i++)ans[i]=n;//如果只用了少于cur次就可以完全存活,剩余操作次数均为cur
}
for(int i=0;i<=n;i++)cout<<ans[i]<<" ";
}
