• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
CodeForces 427B Prison Transfer (滑动窗口)

题意:给定 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;
}

 

posted on 2016-07-24 14:10  dwtfukgv  阅读(448)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3