题意:给定 n, t, c 和 n 个数,问你在这 n 个数中有多少连续的 c 个数,并且这个 c 个数不大于 t。
析:很简单么,是滑动窗口,从第一个开始遍历,如果找到 c 个数,那么让区间前端点加1,如果找不到,那么就区间前端等于后区间+1.
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 15;
const int INF = 0x3f3f3f3f;
int a[maxn];
int main(){
int n, t, c;
while(cin >> n >> t >> c){
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
int s = 0, e = 0;
int cnt = 0;
int ans = 0;
while(e < n){
while(a[e] <= t && cnt < c && e < n) ++cnt, ++e;
if(cnt == c){
--cnt;
++ans;
}
else{
++e;
cnt = 0;
}
}
cout << ans << endl;
}
return 0;
}
浙公网安备 33010602011771号