CodeForces 920A Water The Garden (C)

本文探讨了一种浇水策略,通过多个水龙头同时工作,确保整个花园在最短时间内得到充分灌溉。文章提供了两种算法实现方式,一种适用于复杂场景,另一种则更加简洁高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目
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 n. k 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 ≤ 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.

题意:水龙头浇水,1个单位时间往外扩散一块地(所有水龙头同时扩散),求能给所有地浇上水的最短时间

 //复杂版
 #include <stdio.h>
int main()
{
	int t,n,k,s[200],i,x[200],max;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&k);
		for(i=0;i<k;i++)
			scanf("%d",&x[i]); 
		for(i=0;i<k;i++)
		{
			if(i==0)       // 如果是第一个水龙头        
			{
			    if(k==1)      // 如果只有1个水龙头          
		            s[0]=x[0]>(n-x[0]+1)?x[0]:(n-x[0]+1);
		        else
		            s[0]=x[0]>((x[1]-x[0])/2+1)?x[0]:((x[1]-x[0])/2+1);
		    }
		    else if(i>0&&i<k-1)
		    	s[i]=((x[i]-x[i-1])/2+1)>((x[i+1]-x[i])/2+1)?((x[i]-x[i-1])/2+1):((x[i+1]-x[i])/2+1);
		    else if(i==k-1)      // 如果是最后一个水龙头
		        s[i]=((x[i]-x[i-1])/2+1)>(n-x[i]+1)?((x[i]-x[i-1])/2+1):(n-x[i]+1);
	    }
	    max=s[0];
	    for(i=0;i<k;i++)
		if(max<s[i]) 
		    max=s[i];
	    printf("%d\n",max);
	} 
	return 0;
}
//简单版
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
	int t,n,k,x[210];
	scanf("%d",&t);
	while(t--)
	{
		int maxt=0;
		scanf("%d%d",&n,&k);
		for(int i=0;i<k;i++)
		{
			scanf("%d",&x[i]);
			if(i==0) maxt=x[i];
			if(i==k-1) maxt=max(maxt,n-x[i]+1);
			if(i>0&&i<=k-1) maxt=max(maxt,(x[i]-x[i-1])/2+1);
		}
		printf("%d\n",maxt);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值