Problem Description
Farmer John has two feuding herds of cattle, the Moontagues and the Cowpulets. One of the bulls in the Moontague herd, Romeo, has fallen in love with Juliet, a Cowpulet. Romeo would like to meet with Juliet, but he doesn't want the other members of the Cowpulet herd to find out.
Romeo and Juliet want to meet and graze in as large a region as possible along the pasture fence. However, this region should not contain too many Cowpulets, otherwise the chance of the two being caught is too great. Romeo has determined where along the fence each of the N (1 <= N <= 1000) members of the Cowpulet herd grazes. The long straight fence contains P (1 <= P <= 1000) equally-spaced posts numbered 1..P. Each Cowpulet grazes between some pair of adjacent posts. Help Romeo determine the length of the largest contiguous region along the fence containing no more than C (0 <= C <= 1000) members of the Cowpulet herd.
Input
* Line 1: Three separated integers: N, P, and C
* Lines 2..N+1: Each line contains an integer X, in the range 1..P-1, specifying that a member of the Cowpulet herd grazes between fence posts X and X+1. Multiple Cowpulets can graze between any given pair of fence posts.
Output
* Line 1: A single integer specifying the size (the number of gaps between fence posts) of the largest contiguous region containing at most C Cowpulets.
Sample Input
2 6 1
2
3Sample Output
3
题意:有 n 头牛,一个长度为 p 的道路,每头牛在区间 [x,x+1] 中,问最多包含 c 头牛的道路长度
思路:桶排记录,然后暴力即可,需要注意的是,牛可能会在同一个区间
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int bucket[N];
int main() {
int n;
int p;
int c;
scanf("%d%d%d",&n,&p,&c);
for(int i=1; i<=n; i++) {
int x;
scanf("%d",&x);
bucket[x]++;
}
int res=0;
for(int i=1; i<p; i++) {
int sum=0;
int cnt=0;
for(int j=i; j<p; j++) {
if(bucket[j])
cnt+=bucket[j];
if(cnt>c)
break;
sum++;
}
res=max(sum,res);
}
printf("%d\n",res);
return 0;
}