社区讨论
站外题目求助
题目总版参与者 2已保存回复 15
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 15 条
- 当前快照
- 1 份
- 快照标识符
- @m0g9jegi
- 此快照首次捕获于
- 2024/08/30 13:18 2 年前
- 此快照最后确认于
- 2024/08/30 16:07 2 年前
CPP
#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;
}
思路是双指针来找到最小的满足条件的区间
回复
共 15 条回复,欢迎继续交流。
正在加载回复...