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

相关推荐
Jerry2 小时前
LeetCode 151. 反转字符串中的单词
算法
gugucoding2 小时前
31. 【C语言】堆栈与队列的实现
c语言·开发语言·数据结构·链表
ChaoZiLL4 小时前
我的数据结构3——链表(link list)
数据结构·链表
a1117764 小时前
LM 算法迭代过程动画演示(SLAM)
算法
头茬韭菜4 小时前
Context 的生死抉择:四层压缩、截断算法与 Session Memory
算法·ai
Jerry4 小时前
LeetCode 541. 反转字符串 II
算法
Jerry5 小时前
LeetCode 344. 反转字符串
算法
cookies_s_s5 小时前
C++ 字符串动态创建对象 -- 工厂模式、自动注册、模板递归动态调用
服务器·开发语言·c++
搞科研的小刘选手5 小时前
【香港大学主办&IEEE出版】第六届计算机视觉、应用与算法国际学术会议(CVAA 2026)
算法·计算机视觉·应用·学术会议
Java小白笔记5 小时前
Codex config.toml配置实战指南
人工智能·算法·chatgpt·ai编程·集成学习