Beijing Guards

6 篇文章 0 订阅
3 篇文章 0 订阅

题目描述

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.

输入

The input contains several blocks of test eases. Each case begins with a line containing a single integer ln100000, 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.

输出

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.

样例输入:

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

样例输出:

8
5
3

写这道题的过程真是一言难尽,一直提交一直wrong。。。。。。
后来边输入边判断就过了。。。。。。

上代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
const int inf = 0x3f3f3f3f;
using namespace std;
int n;
const int MAX = 2e5 + 5;
int a[MAX];
int l[MAX],r[MAX];
bool solve(int what)
{
	long long left=a[1];
	long long right=what-a[1];
	l[1]=a[1];
	r[1]=0;
	for(int i=2;i<=n;i++)
	{
		if(i%2==1)
		{
//			r[i]=min(right-r[i-1],a[i]);
			if(right-r[i-1]<a[i])r[i]=right-r[i-1];
			else r[i]=a[i];
			l[i]=a[i]-r[i];
		}
		else
		{
//			l[i]=min(left-l[i-1],a[i]);
			if(left-l[i-1]<a[i])l[i]=left-l[i-1];
			else l[i]=a[i];
			r[i]=a[i]-l[i];
		}
	}
	return l[n]==0;
}
int  main()
{
	while(1)
	{
		scanf("%d",&n);
		if(n==0)
		{
			break;
		}
		int i,j;
		int maxx = 0;
		for(int i = 1; i<=n; i++) {
			scanf("%d",&a[i]);
			maxx = max(maxx,a[i]+a[i-1]);
		}
		maxx = max(maxx,a[1]+a[n]);
		if(n%2==0)
		{
			printf("%d\n",maxx);
		}
		else
		{
			if(n==1)
			{
				printf("%d\n",a[1]);
			}
			else
			{
				int lb=maxx,ub=inf,mid;
				while(lb < ub) {
				mid = (lb+ub)>>1;
				if(solve(mid)) ub = mid;
				else lb = mid+1;
				}
				printf("%d\n",lb);
			}
		}
	}
	return 0;
}


一道二分+贪心的题目,在输入为偶数是较为容易判断,为奇数时,就比较难想到了
以第一个所需要的数目来解析,
如果一组数1 2 3 4 5 6 7 8 9
第一个需要的个数为3
那么就以1 2 3 这三个数为左方,其余为右方,判断到最后一个,是否在没有使用左方数字的情况下,完成奖励分配即可
使用二分,找到那个最小的值

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值