B. Prison Transfer
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
Copy
4 3 3
2 3 1 1
output
Copy
2
input
Copy
1 1 1
2
output
Copy
0
input
Copy
11 4 2
2 2 0 7 3 2 2 4 9 1 4
output
Copy
6
这题大概意思就是,给n t c,在n个数据中连续c个最大值小于t的个数,这题用RMQ可以 做,很明显线段树也是可以做的。RMQ:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,t,c;
int ans;
int RMQ_max[200010][20];
double Limit;
void init(int n){
Limit = log(n)/log(2.0);
int tmp;
for(int i=1;i<=n;i++){
scanf("%d",&tmp);
RMQ_max[i][0] = tmp;
}
for(int i=1;i<=Limit;i++){
for(int j=1;j+(1<<i)-1<=n;j++){
RMQ_max[j][i] = max(RMQ_max[j][i-1],RMQ_max[j+(1<<(i-1))][i-1]);
}
}
}
void RMQ_find(){
for(int i=1;i<=n-c+1;i++){
int l = i;
int r = i+c-1;
int k = (int)(log(r-l+1)/log(2.0));
int MAXN = max(RMQ_max[l][k],RMQ_max[r-(1<<k)+1][k]);
if(MAXN <= t) ans++;
}
}
int main(){
while(~scanf("%d%d%d",&n,&t,&c)){
init(n);
ans = 0;
RMQ_find();
printf("%d\n",ans);
}
return 0;
}
//2 2 0 7 3 2 2 4 9 1 4
线段树做法:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int mx[maxn*4];
int n,t,c;
void build(int l,int r,int id)
{
if(l==r)
{
int x;
scanf("%d",&x);
mx[id]=x;
return ;
}
int mid=(l+r)>>1;
build(l,mid,id<<1);
build(mid+1,r,id<<1|1);
mx[id]=max(mx[id<<1],mx[id<<1|1]);
}
int qu(int l,int r,int ql,int qr,int id)
{
if(ql<=l&&r<=qr)
{
return mx[id];
}
int mid=(l+r)>>1;
int ans=0;
if(ql<=mid) ans=max(ans,qu(l,mid,ql,qr,id<<1));
if(qr>mid) ans=max(ans,qu(mid+1,r,ql,qr,id<<1|1));
return ans;
}
int main()
{
scanf("%d%d%d",&n,&t,&c);
build(1,n,1);
int sum=0;
for(int i=1;i+c-1<=n;i++)
{
int ans=qu(1,n,i,i+c-1,1);
// printf("%d\n",ans);
if(ans<=t) sum++;
}
printf("%d",sum);
}