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 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
郝学胜-神的一滴3 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
AvocadoZeroOneOI4 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz38996 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
whitelbwwww6 小时前
cuBLAS--CUDA高性能线性代数计算库--第一部分
c++·线性代数·gpu算力·cuda c++
console.log('npc')8 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
eggwyw8 小时前
C++设计模式之解释器模式
开发语言·c++
橘子139 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘20 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
我不会插花弄玉1 天前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll