poj 2356 鸽巢原理

来源:互联网 发布:主流的社交软件 编辑:程序博客网 时间:2024/05/16 11:23

Romeo Meets Juliet
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1062 Accepted: 489

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 123

Sample Output

3

Source

USACO 2003 Fall Orange

题目大意就是先给出一个数N,接着再给出N个数,要你从这N个数中任意选择1个或多个数,使得其和是N的倍数

如果找不到这样的答案 则输出0

答案可能有多个,但智勇任意输出一个解就行。

输出的第一行是选择元素的个数M,接着M行分别是选择的元素的值

/* 因为根据鸽巢原理可以知道这题一定是有解的 因为当求出所有sum后 如果有sum%n等于0  那么直接输出这段即可如果都不等于0  那么n个sum里面一定有重复的sum值 那么这2个重复之间的数据的和就是n的倍数那么这样的话 一定存在解  所以如果一个题目满足鸽巢原理就不用再去考虑是否是任意取的了*/#include<stdio.h>#include<stdlib.h>struct haha{int sum_val;int pos;}a[10010];int b[10010];int cmp(const void *a,const void *b){return (*(struct haha *)a).sum_val-(*(struct haha *)b).sum_val;}int main(){int  n,i,j;while(scanf("%d",&n)!=EOF){a[0].sum_val=a[0].pos=0;for(i=1;i<=n;i++){              scanf("%d",&b[i]);  a[i].pos=i;              a[i].sum_val=(a[i-1].sum_val+b[i])%n;  //printf("sum_vas=%d\n",a[i].sum_val);}qsort(a+1,n,sizeof(a[1]),cmp);for(i=1;i<=n;i++){if(a[i].sum_val==0){    printf("%d\n",a[i].pos);for(j=1;j<=a[i].pos;j++)    printf("%d\n",b[j]);break;}if(i<n&&a[i].sum_val==a[i+1].sum_val) {printf("%d\n",a[i+1].pos-a[i].pos);for(j=a[i].pos+1;j<=a[i+1].pos;j++)printf("%d\n",b[j]);break;}}if(i>n) printf("0\n");}return 0;}


原创粉丝点击