【Jason's_ACM_解题报告】Beijing Guards

Beijing Guards

Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads. The walls were protected by guard towers, and there was a guard living in each tower. The wall can be considered to be a large ring, where every guard tower has exaetly two neighbors.

The guard had to keep an eye on his section of the wall all day, so he had to stay in the tower. This is a very boring job, thus it is important to keep the guards motivated. The best way to motivate a guard is to give him lots of awards. There are several different types of awards that can be given: the Distinguished Service Award, the Nicest Uniform Award, the Master Guard Award, the Superior Eyesight Award, etc. The Central Department of City Guards determined how many awards have to be given to each of the guards. An award can be given to more than one guard. However, you have to pay attention to one thing: you should not give the same award to two neighbors, since a guard cannot be proud of his award if his neighbor already has this award. The task is to write a program that determines how many different types of awards are required to keep all the guards motivated.


Sample Input

The input contains several blocks of test eases. Each case begins with a line containing a single integer 1<=n<=100000, the number of guard towers. The next n lines correspond to the n guards: each line contains an integer, the number of awards the guard requires. Each guard requires at least 1, and at most l00000 awards. Guard i and i + 1 are neighbors, they cannot receive the same award. The first guard and the last guard are also neighbors.

The input is terminated by a block with n = 0.


Sample Output

For each test case, you have to output a line containing a single integer, the minimum number x of award types that allows us to motivate the guards. That is, if we have x types of awards, then we can give as many awards to each guard as he requires, and we can do it in such a way that the same type of award is not given to neighboring guards. A guard can receive only one award from each type.


Input
3
4
2
2
5
2
2
2
2
2
5
1
1
1
1
1
0


Output
8
5
3


由于此题答案要求最小值,于是联想到了二分法,而要使用二分法,对于此题来说首先要保证存在一值点,在值点左边的所有值均不满足答案要求,而右侧所有值均满足,很显然,由于award越多,每个guard所分的award不同的概率就越大,所以满足这一性质。

证:满足二分性。

1.对于k个award来说,若n个guard分完k个后,第m个guard还需要x个award才能保证合法,如果此时从k个award中拿走一个,那么为了保证最好的分配方式,只能从第m个guard手中拿走一个award,此时第m个guard还需要x+1个award才能保证合法,显然不能满足。

2.对于k个award来说,若n个guard分完k个后合法,如果此时在加入一个award,那么如果我们将这个award和任意的一个guard手中的award换一下的话,发现这样仍然合法,因为新来的award是独一无二的,而它又只有一个guard持有,显然满足。

所以满足二分性。


接下来要考虑的就是二分法中的check函数怎么写了,对于这个题来说,也就是说是如何分配礼物判断其是否合法。这一点Liu已有了很好的解释(其实想证明呢,可是证了半天证不出来,只能凭借着直觉和多试几组数据来得出结论的正确性,希望Doris美眉能有好的想法)。


附代码如下:

#include<cstdio>
#include<algorithm>

using namespace std;

#define MAXN (100000+5)

int r[MAXN],left[MAXN],right[MAXN];
int n;

bool check(int p){
	int lt=r[1],rt=p-lt;
	left[1]=lt;
	right[1]=0;
	for(int i=2;i<=n;i++){
		if(i%2==0){
			left[i]=min(lt-left[i-1],r[i]);
			right[i]=r[i]-left[i];
		}else{
			right[i]=min(rt-right[i-1],r[i]);
			left[i]=r[i]-right[i];
		}
	}
	if(left[n]==0)return true;
	return false;
}

int main(){
	while(scanf("%d",&n)==1&&n){
		
		for(int i=1;i<=n;i++)scanf("%d",&r[i]);
		if(n==1){printf("%d\n",r[1]);continue;}
		r[n+1]=r[1];
		
		int L=0,R=0;
		for(int i=1;i<=n;i++)L=max(L,r[i]+r[i+1]);
		if(n%2==1){
			for(int i=1;i<=n;i++)R=max(R,r[i]*3);
			while(L<R){
				int MID=((L+R)>>1);
				if(check(MID))R=MID;
					else L=MID+1;
			}
		}
		printf("%d\n",L);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值