p1373 奶牛的卧室

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/84970518

题目

描述 Description
奶牛们有一个习惯,那就是根据自己的编号选择床号。如果一头奶牛编号是a,并且有0…k-1一共k张床,那么她就会选择a mod k号床作为她睡觉的地点。显然,2头牛不能睡在一张床上。那么给出一些奶牛的编号,请你为她们准备一间卧室,使得里面的床的个数最少。

输入格式 Input Format
第一行是奶牛的个数n(1<=n<=5000);第2到第n+1行是每头奶牛的编号Si(1<=Si<=1000000)。

输出格式 Output Format
仅一行,是最少的床的数目。
样例输入 Sample Input

5
4
6
9
10
13

样例输出 Sample Output

8
时间限制 Time Limitation
1s

代码

#include<bits/stdc++.h>
using namespace std;
const int maxnum=1001000;
long long n,k,a[maxnum];
bool v[maxnum];
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	while (ch>='0'&&ch<='9') { num=(num<<1)+(num<<3)+ch-'0'; ch=getchar(); }
	return num*f;
}
int main()
{
	n=read();
	for (int i=1;i<=n;i++)
		a[i]=read();
	sort(a+1,a+n+1);
	for (int i=1;i<n;i++)
		for (int j=i+1;j<=n;j++)
			v[ a[j]-a[i] ]=1;
	int h;
	for (int i=2;i<=a[n];i++)
	{
		bool flag=false;
		if (v[i]) continue;
		h=i*2;
		while (h<a[n])//判断这个数的倍数是不是任意两个数的差
		{
			if (v[h])
			{
				flag=true;
				break;
			}
			h+=i;
		}
		if (!flag)
		{
			k=i;
			break;
		}
	}	
	cout<<k<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/84970518
今日推荐