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 变换区间就在这两个之间

相关推荐
沐雪轻挽萤1 小时前
6. C++17新特性-编译期 if 语句 (if constexpr)
开发语言·c++
水云桐程序员1 小时前
C语言编程基础,输入与输出
c语言·开发语言·算法
ZPC82102 小时前
MoveIt Servo 与自己编写的 Action Server 通信
人工智能·算法·机器人
jllllyuz2 小时前
采用核函数的极限学习机(KELM)MATLAB实现
算法
apcipot_rain2 小时前
【天梯赛】2026天梯赛模拟赛——题解
开发语言·c++·算法·蓝桥杯·天梯赛
-To be number.wan2 小时前
重新认识一下“私有继承”
c++·学习
江奖蒋犟2 小时前
【C++】红黑树
开发语言·c++
lclin_20202 小时前
【大恒相机】C++ 设备枚举+打开关闭+启停采集(基础入门)
c++·工业相机·大恒相机·galaxysdk
.柒宇.2 小时前
力扣hot100之最大子数组和(Java版)
数据结构·算法·leetcode
黎阳之光2 小时前
非视距·自愈·广覆盖|黎阳之光1.4&5.8GHz宽带自愈网无线基站,重构工业级无线通信
大数据·人工智能·算法·安全·数字孪生