codeforces 920A Water The Garden

A. Water The Garden

It is winter now, and Max decided it's about time he watered the garden.

The garden can be represented as n consecutive garden beds, numbered from 1 to nk beds contain water taps (i-th tap is located in the bed xi), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed xi is turned on, then after one second has passed, the bed xi will be watered; after two seconds have passed, the beds from the segment [xi - 1, xi + 1] will be watered (if they exist); after j seconds have passed (j is an integer number), the beds from the segment [xi - (j - 1), xi + (j - 1)] will be watered (if they exist). Nothing changes during the seconds, so, for example, we can't say that the segment [xi - 2.5, xi + 2.5] will be watered after 2.5 seconds have passed; only the segment [xi - 2, xi + 2] will be watered at that moment.

The garden from test  1. White colour denotes a garden bed without a tap, red colour — a garden bed with a tap.
The garden from test  1 after  2 seconds have passed after turning on the tap. White colour denotes an unwatered garden bed, blue colour — a watered bed.

Max wants to turn on all the water taps at the same moment, and now he wonders, what is the minimum number of seconds that have to pass after he turns on some taps until the whole garden is watered. Help him to find the answer!

Input

The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 200).

Then t test cases follow. The first line of each test case contains two integers n and k (1 ≤ n ≤ 2001 ≤ k ≤ n) — the number of garden beds and water taps, respectively.

Next line contains k integers xi (1 ≤ xi ≤ n) — the location of i-th water tap. It is guaranteed that for each  condition xi - 1 < xiholds.

It is guaranteed that the sum of n over all test cases doesn't exceed 200.

Note that in hacks you have to set t = 1.

Output

For each test case print one integer — the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered.

Example
input
3
5 1
3
3 3
1 2 3
4 1
1
output
3
1
4

题意:给你编号1~n的花园,花园从左到右排成一排。其中有k个花园有水龙头,当水龙头所在的花园浇满后,会流向左右相邻花园,现在同时打开所有水龙头,问你多长时间后n个花园能被浇满。

思路:这个题目很有意思,只用把握一点就好:所有水龙头同时开启,所有水流同时流动。所以先看边界情况,从编号最小的水龙头到最左端,这一段只有一个方向的水流,最右端也是同理,这两个边界都只有一个方向的水流,而且它们同时流动,所以边界所花时间是两者中较大的:ans=max(a[1]-1,n-a[k])。然后是中间部分,一定是来自两个方向的水流,此时要看间隔的个数x,如果x是奇数,那么时间就是x/2+1,偶数就是x/2,这个自己想一下就能想明白,而且所有中间间隔的双向水流都是同时流动,所以依然是取最大值即可,而不是累加。

一开始想对了,写的时候忘了同时流动的条件,wa了几次。。。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[110],n,k;
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&k);
        for(ll i=1;i<=k;i++)scanf("%lld",&a[i]);
        ll ans=max(a[1]-1,n-a[k]);         //处理边界的单向水流
        for(ll i=2;i<=k;i++)
        {
            ll tmp=a[i]-a[i-1]-1;
            if(tmp&1)tmp=tmp/2+1;
            else tmp/=2;
            ans=max(ans,tmp);            //更新中间的双向水流
        }
        printf("%lld\n",ans+1);
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值