div 2 c Binary String Copying

cpp 复制代码
#include<bits/stdc++.h>
using namespace std ;
using ll = long long;
using PII = pair<int,int>;
string s;
void solve(){
    int n,m;
    cin >> n >> m;
    //vector<int>s(n+1);
    vector<int>ne1(n+2,n+1);
    vector<int>pre0(n+2,0);
    cin>>s;
    s = " " + s;
    for(int i=1;i<=n;i++){
        pre0[i] = pre0[i-1];
        if(s[i] == '0'){
            pre0[i] = i;
        }
    }
    for(int i=n;i>=1;i--){
        ne1[i] = ne1[i+1];
        if(s[i] == '1'){
            ne1[i] = i;
        }
    }
    set<PII> s;
    while(m--){
        int l,r;
        cin>>l>>r;
        //cout<<ne1[l]<<" "<<pre0[r]<<endl;
        if(ne1[l] > pre0[r]){
            s.emplace(-1,-1);
        }else{
            s.emplace(ne1[l],pre0[r]);
        }
    }
    cout<<s.size();

}



int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
        puts("");
    }
}

用最小的变换区间代表整个大区间

找到L右边最近的1 R左边最近的0 变换区间就在这两个之间

相关推荐
ShineWinsu7 小时前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
数模竞赛Paid answer8 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆8 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
Lzg_na9 小时前
constexpr的优势
c++
想吃火锅100510 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
王维同学10 小时前
进程模块枚举、映像身份与线程启动地址关联
c++·windows·安全
hold?fish:palm10 小时前
24 回文链表
数据结构·c++·链表
举手10 小时前
Dispatcher模块剖析
linux·c++
Nil20810 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣11 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论