Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

B. Prison Transfer

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/427/B

Description

The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.

For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.

Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are,

    The chosen c prisoners has to form a contiguous segment of prisoners.
    Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer.

Find the number of ways you can choose the c prisoners.

Input

The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.

Output

Print a single integer — the number of ways you can choose the c prisoners.

Sample Input

4 3 3
2 3 1 1

Sample Output

2

HINT

 

题意

给你n个人,让你选出连续c个人,要求这c个人的最大值小于t

问你有多少种选择方法

题解:

正解大概双端队列吧

我写的线段树

代码

 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1050005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

struct node
{
    int l,r;
    int ma;
};
node a[maxn*4];
int num[maxn];
void build(int x,int l,int r)
{
    a[x].l=l,a[x].r=r;
    if(l==r)
    {
        a[x].ma=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    a[x].ma=max(a[x<<1].ma,a[x<<1|1].ma);
}
int query(int x,int l,int r)
{
    int L=a[x].l,R=a[x].r;
    if(l<=L&&R<=r)
        return a[x].ma;
    int mid=(a[x].l+a[x].r)>>1;
    if(r<=mid)
        return query(x<<1,l,r);
    if(l>mid)
        return query(x<<1|1,l,r);
    return max(query(x<<1,l,mid),query(x<<1|1,mid+1,r));
}
int main()
{
    int n=read(),t=read(),c=read(),ans=0;
    c--;
    for(int i=1;i<=n;i++)
        num[i]=read();
    build(1,1,n);
    for(int i=1;i<=n-c;i++)
        if(query(1,i,i+c)<=t)
            ans++;
    cout<<ans<<endl;
}

 

你可能感兴趣的:(codeforces)