站外题目求助
  • 板块题目总版
  • 楼主XiaoYao789
  • 当前回复15
  • 已保存回复15
  • 发布时间2024/8/30 13:16
  • 上次更新2024/8/30 16:07:00
查看原帖
站外题目求助
332421
XiaoYao789楼主2024/8/30 13:16

题目链接

#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N=1e7+10;
int n,a,b,red[N],yellow[N],white[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        red[i]=red[i-1]+(x==1);
        yellow[i]=yellow[i-1]+(x==2);
        white[i]=white[i-1]+(x==0);
    }
    int l=1,r=n;
    while(red[r]-red[l]>=a&&yellow[r]-yellow[l]>=b) l++;
    while(red[r-1]>=a-red[l-1]&&yellow[r-1]-yellow[l-1]>=b) r--;
    cout<<white[r]-white[l-1]<<endl;
    return 0;
}

思路是双指针来找到最小的满足条件的区间

2024/8/30 13:16
加载中...