P1154 cow stables points

Title Description

Farmer John has N (1≤N≤5000) cows, each cow has a unique number different from the other cows si, all cows are sleeping in a barn in the K stables, stables numbered 0 to K-1. Each cow knows himself to sleep in the stables which, as John taught them to do division, SimodK value is the head of the milk year of sleeping iii stable number.

A given number of cows, determine the smallest K such that no more than two, or two sleeping in the same cows in stables.

Input Format

The first line of a positive integer N, N + 1 to the second line of each row represents an integer number of the cow.

Output Format

An integer representing the required minimum of K, so that all of the test data K must exist.

Sample input and output

Input # 1
5 
4 
6 
9 
10 
13 
Output # 1
8

Description / Tips

And (1≤Si≤1000000)

Thinking

c%b=(a%b+(c+a)%b)%b

That is, when k is a difference between the two numbers, there will be two cows living in the same stables

So long as we find the difference of all numbers, and then search from small to large, as long as this number is not any difference between the two cows, outputs

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=1000010;

int n,ans;
int a[N],b[N];

int main () {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)
		scanf("%d",&a[i]);
	memset(b,0,sizeof(b));
	for(int i=1; i<=n; i++)
		for(int j=i+1; j<=n; j++)
			b[abs(a[j]-a[i])]=1;
	ans=n;
	while(b[ans]==1)
		ans++;
	printf("%d\n",ans);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11802266.html