Run For The Prize(拿走所有的奖杯)

Run For The Prize

You and your friend are participating in a TV show "Run For Your Prize".

At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You start at position 1, your friend — at position 106 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order.

You know that it takes exactly 1 second to move from position x to position x + 1 or x - 1, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend's speed) at all.

Now you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend.

What is the minimum number of seconds it will take to pick up all the prizes?

Input

The first line contains one integer n (1 ≤ n ≤ 105) — the number of prizes.

The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 106 - 1) — the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order.

Output

Print one integer — the minimum number of seconds it will take to collect all prizes.

Examples
Input
3
2 3 9
Output
8
Input
2
2 999995
Output
5
Note

In the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8.

In the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5.



刚开始我的思路:比较两个人要拿同一奖杯所需时间,选择时间最短的那个人1去拿那个奖杯,然后这个人1向前移动了一定的距离,人2仍在最末位置(未移动),然后继续进行比较,选择时间最短的那个人去拿奖杯,一次类推,一直到所有的奖杯都拿完,比较两个拿奖杯所用时间,选取时间大的输出。

#include<stdio.h>
long long k[110000];
int main()
{
	long long n;
	while(scanf("%lld",&n)!=EOF)
	{
		int i,t=1;
		long long sum1=0,sum2=0;
		long long L=1000000;
		for(i=0;i<n;i++)
			scanf("%lld",&k[i]);
		long long temp1=k[n-1]-t;
		long long temp2=L-k[0];
		for(i=0;i<n;i++)
		{
			if(k[i]-t<=L-k[i]){
				sum1+=(k[i]-t);
				t=k[i];
			}
			else{
				sum2+=(L-k[i]);
				L=k[i];
			}
		}
		long long max; 
		if(sum1<sum2) max=sum2;
		else max=sum1; 
		printf("%lld\n",max);
	}
	return 0;
}

后来继续读题发现,如果这样想的话,两个人不是同时进行这项事情的,那么,如何实现两个人同时进行意见事情?我觉得可以用max和min来写,先判断当前所取奖杯所用时间最短的人,然后再与已用时间比大小,选择较大的,以此去取奖杯,直到去玩为止。max(temp,min(ren1,ren2))。这个可以ac

#include<stdio.h>
#define L 1e6
int a[1000000];
int min(int a,int b)
{
	return a<b?a:b;
}
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int temp=0;
		int i;
		for(i=0;i<n;i++)
		  scanf("%d",&a[i]);
		for(i=0;i<n;i++)
		{
			temp=max(temp,min(L-a[i],a[i]-1));
		}
		printf("%d\n",temp);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41818544/article/details/80613767
run