跳到正文

题解

题解:CF2042C Competitive Fishing

_Ch1F4N_约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
CF2042C

版本

  1. v1当前

贡献摊到每次分割两段的操作上。

假若段之间的分界点为 p1,p2,...,pkp_1,p_2,...,p_k,那么相当于每次给 [pi,n][p_i,n] 内所有数加上 11 的贡献系数,求出每个位置的贡献的后缀和后在每个位置进行分割的代价都是已知的,从大往小贪心即可。

注意不能在位置 11 处分割。

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+114;
int n,k;
void work(){
    cin>>n>>k;
    string S;
    cin>>S;
    priority_queue<int> q;
    int sum=0;
    for(int i=S.size()-1;i>=1;i--){
        if(S[i]=='1') sum++;
        else sum--;
        q.push(sum);
    }
    int ans=0;
    while(q.size()>0){
        k-=q.top();
        if(q.top()<=0) break;
        ans++;
        if(k<=0){
            cout<<ans+1<<'\n';
            return ;
        }
        q.pop();
    }
    cout<<"-1\n";
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--) work();
    return 0;
}
原文