poj 2356 鸽巢原理

Romeo Meets Juliet
Time Limit: 1000MS Memory Limit: 65536K
Total 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 1
2
3

Sample Output

3

Source


题目大意就是先给出一个数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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值