洛谷CF920A Water The Garden

题意翻译

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

感谢@伏轩彤666 提供的翻译

题目描述

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

The garden can be represented as nn consecutive garden beds, numbered from 11 to nn . kk beds contain water taps ( ii -th tap is located in the bed x_{i}xi​ ), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed x_{i}xi​ is turned on, then after one second has passed, the bed x_{i}xi​will be watered; after two seconds have passed, the beds from the segment [x_{i}-1,x_{i}+1][xi​−1,xi​+1] will be watered (if they exist); after jj seconds have passed ( jj is an integer number), the beds from the segment [x_{i}-(j-1),x_{i}+(j-1)][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 [x_{i}-2.5,x_{i}+2.5][xi​−2.5,xi​+2.5] will be watered after 2.52.5 seconds have passed; only the segment [x_{i}-2,x_{i}+2][xi​−2,xi​+2] will be watered at that moment.

 The garden from test 11 . White colour denotes a garden bed without a tap, red colour — a garden bed with a tap.  The garden from test 11 after 22seconds 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!

输入输出格式

输入格式:

 

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

Then tt test cases follow. The first line of each test case contains two integers nn and kk ( 1<=n<=2001<=n<=200, 1<=k<=n1<=k<=n ) — the number of garden beds and water taps, respectively.

Next line contains kk integers x_{i}xi​ ( 1<=x_{i}<=n1<=xi​<=n ) — the location of ii -th water tap. It is guaranteed that for each  condition x_{i-1}<x_{i}xi−1​<xi​ holds.

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

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

 

输出格式:

 

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.

 

输入输出样例

输入样例#1: 复制

3
5 1
3
3 3
1 2 3
4 1
1

输出样例#1: 复制

3
1
4

说明

The first example consists of 33 tests:

  1. There are 55 garden beds, and a water tap in the bed 33 . If we turn it on, then after 11 second passes, only bed 33 will be watered; after 22 seconds pass, beds [1,3][1,3] will be watered, and after 33 seconds pass, everything will be watered.
  2. There are 33 garden beds, and there is a water tap in each one. If we turn all of them on, then everything will be watered after 11 second passes.
  3. There are 44 garden beds, and only one tap in the bed 11 . It will take 44 seconds to water, for example, bed 44 .

第一次写BFS,写了很久,很难,注意先在纸上模拟队列的进出:

#include<bits/stdc++.h>
using namespace std;
bool vis[205];//花台,是否浏览过花台 
int ans,tim[205];//答案,流到花台i所需要的时间为tim [i]
int n,k,locat;
queue<int> Q;

void BFsearch(int x)
{
	if(Q.empty()==1) return ;
	
	int temp=Q.front();
	Q.pop();
	
	ans=max(ans,tim[temp]);
	
	if(temp-1>=1&&vis[temp-1]==0)
	{
		vis[temp-1]=1;
		tim[temp-1]=tim[temp]+1;
		Q.push(temp-1);
	}
	
	if(temp+1<=n&&vis[temp+1]==0)
	{
		vis[temp+1]=1;
		tim[temp+1]=tim[temp]+1;
		Q.push(temp+1);
	}
	
	BFsearch(Q.front());
}

int main()
{
	int test;
	cin>>test;
	while(test--)
	{
		memset(vis,0,sizeof(vis));
		memset(tim,0,sizeof(tim));
		while(!Q.empty()) Q.pop();
		
		ans=-1;
		cin>>n>>k;//花台的个数 ,喷头的个数 
		
		for(int i=1;i<=k;i++)
		{
			cin>>locat;// 喷头的位置 
			tim[locat]=1;//需要1时间 
			vis[locat]=1;
			Q.push(locat);
		}
		
		BFsearch(Q.front());
		cout<<ans<<endl;
	}
	return 0;
} 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值