CF920A Water The Garden (#搜索 -2.3)(#模拟 -1.12)

Jesus Christ, I'm alone again.

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 nkbeds 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 1after 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 nand k (1 ≤ n ≤ 200, 1 ≤ 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 < xi holds.

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

Note

The first example consists of 3 tests:

1、There are 5 garden beds, and a water tap in the bed 3. If we turn it on, then after 1 second passes, only bed 3 will be watered; after 2 seconds pass, beds [1, 3] will be watered, and after 3 seconds pass, everything will be watered.

2、There are 3 garden beds, and there is a water tap in each one. If we turn all of them on, then everything will be watered after 1 second passes.

3、There are 4 garden beds, and only one tap in the bed 1. It will take 4 seconds to water, for example, bed 4.


思路

有一个长度为n的花园,一共有k个水龙头,分别在x[1],x[2]...x[k]的位置。第t秒每个水龙头可以浇灌到它的前第t个方格和它后第t个方格,问浇灌完花园的时间。

bfs:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>//队列头文件
using namespace std;
struct node
{
	int x,d;//d记录时间 
}p;
queue <node> qu; 
int main()
{
	ios::sync_with_stdio(false);
	int t,n,k,i,j,a[401],x,maxn;
	queue<node>q;//创造一个队列 
	cin>>t;//t个数据 
	while(t--)
	{
		maxn=0;
		memset(a,0,sizeof(a));
		cin>>n>>k;
		for(i=1;i<=k;i++)
		{
			cin>>x;
			qu.push(node{x,0});//在队尾插入结构体数据 
		}
		while(!qu.empty())//队列不为空 
		{
			p=qu.front();
			qu.pop();
			if(a[p.x]==0)//还没拓展 
			{
				a[p.x]=1;//对每个队列拓展到的入队 
				p.d++;//时间+1 
				if(maxn<p.d)//寻找最短的时间 
					maxn=p.d;
				if(p.x+1<=n)
					qu.push(node{p.x+1,p.d});
				if(p.x-1>=1)//两边同时找
					qu.push(node{p.x-1,p.d});
            }
        }
		cout<<maxn<<endl;
	}
	return 0;
}

模拟+递推:

#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;
}

猜你喜欢

转载自blog.csdn.net/Apro1066/article/details/81782451
今日推荐