codeforces --- Round #244 (Div. 2) B. Prison Transfer

思路摘自http://www.cnblogs.com/acmer-jsb/p/3707989.html

这题思路还是很巧妙的,遍历一遍,碰到超出限制的数再进行判断即可

 1 #include <stdio.h>

 2 #include <iostream>

 3 using namespace std;

 4 int main(){

 5     int n, t, c, i, j, k;

 6     int ans = 0, cnt = 0;

 7     cin >> n >> t >> c;

 8     for(i = 0; i < n; ++i){

 9         scanf("%d",&k);

10         if(k > t){

11             if(cnt >= c)    ans += cnt - c + 1;

12             cnt = 0;

13         }

14         else

15             cnt++;

16     }

17     if(cnt >= c)    ans += cnt - c + 1;

18     cout << ans << endl;

19     return 0;

20 }

 

接下来是我的过了初判最后TLE的CODE:

 #include<iostream>

#include<algorithm>

#include<cstdio>

#include<queue>

using namespace std;

const int MAXSIZE=50002;

const int INF=0x3fffff;

int main(){

    int i, j, k, n, m, tmp,t ,c;

    int pol,thi,ans;

    int start = 0, end;

    int array[222222];

    ans = 0;

    scanf("%d%d%d",&n,&t,&c);

    for(i = 0; i < n; i++)  scanf("%d",&array[i]);

    int flag = 0;

    for(end = start + c - 1; end < n; start++,end++){

        if(!flag){

            for(m = start; m <= end ; m++){

                if(array[m] > t)    break;

            }

            if(m == end + 1){

                ans++;

                flag = 1;

            }

        }

        else{

            if(array[end] <= t) ans++;

            else    flag = 0;

        }

    }

    printf("%d\n",ans);

    return 0;

}


 

你可能感兴趣的:(codeforces)