POJ2189

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
3
Sample Output
3

大意:有一些牛散落在数轴上求 围住指定数目的牛的栏杆的最大长度。题有点不好读做还是比较好做的,从前往后便利即可。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
#include <stdio.h>
#include<map>
using namespace std;
int a[1000008];
int main()
{
    int n,m,k,x;
    while(cin>>n>>m>>k)
    {
        memset(a,0,sizeof(a));
        for(int i=0; i<n; i++)
        {
            cin>>x;
            a[x]++;
        }
        int maxn=-1;
        for(int i=1; i<m; i++)
        {
            int x=0;
            int cnt=0;
            for(int j=i; j<m; j++)
            {
                if(x+a[j]>k)
                {
                    break;
                }
                x+=a[j];
                cnt++;
            }
            maxn=max(maxn,cnt);
        }
        cout<<maxn<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值