题解 | 小红的01子序列构造(easy)| new int 竟然不是初始化为 0 !!!

小红的01子序列构造(easy)

https://www.nowcoder.com/practice/ee0b6c6baa2642c182df8b4390126f9a

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    long long n,k;
    cin>>n>>k;
    cin>>s;
    s=" "+s;
    long long sum=0;
  // 或者 int* cnt=new int[2]();
    int * cnt=new int[2];
    cnt[0]=cnt[1]=0;
    for(int l=1,r=1;r<=n;r++){
        if(s[r]=='1'){
            sum+=cnt[0];
        }
        cnt[s[r]-'0']++;
        while(sum>k){
            cnt[s[l]-'0']--;
            if(s[l++]=='0')sum-=cnt[1];
        }
        // if(r==21079){
        //     cout<<l<<"\n";
        //     return 0;
        // }
        if(sum==k){
            cout<<l<<" "<<r<<"\n";
            return 0;
        }
    }
    cout<<-1<<"\n";
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务