Educational Codeforces Round 38 (Rated for Div. 2) B. Run For Your Prize

B. Run For Your 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 a1a2, ..., 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
Copy
3
2 3 9
output
8
input
Copy
2
2 999995
output
5

题意:你站在1的位置,你的朋友在1e6的位置,现在给你n个奖品的位置,你和你的朋友同时开始跑,问最少几秒拿下所有奖品。

思路:两人从两头往中间跑,时间最多也就是到中间的耗时500000-1,别的方式跑都可能会超过这个,所以往中间跑必然是最优解。

先从1开始,当出现大于500000-1时,记下答案ans1和当前位置p,接着从1e6开始倒着遍历直到p,记下答案ans2,比较两个答案输出大的即可:max(ans1,ans2)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100010],n,l,r,ans1,ans2,p;
int main()
{
    while(~scanf("%lld",&n))
    {
        for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);
        ans1=ans2=0;  
        l=1;r=1e6;  //l是左边的即时位置.r是右边的人的即时位置
        p=-1;
        for(ll i=1;i<=n;i++)
        {
            if(a[i]<=500000)
            {
                ans1+=a[i]-l;
                l=a[i];     //更新即时位置
            }
            else
            {
                p=i;
                break;  
            }
        }
        if(p!=-1)       //如果需要朋友跑,进行倒序遍历
        {
            for(ll i=n;i>=p;i--)
            {
                ans2+=r-a[i];
                r=a[i];
            }
        }
        printf("%lld\n",max(ans1,ans2)); //比较输出答案
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值