uva 1335 Beijing Guards(二分)

uva 1335 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.

Input

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.

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.

Sample Input

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

Sample Output

8
5
3

题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物。如果两个相邻的人拥有同一种礼物,则双方都会很不高兴,问最少需要多少种不同的礼物才能满足所有人的需求。

解题思路(转):

1. 当n为偶数时, 答案是相邻两人之间的的r值最大和, 即p = max{ ri + ri+1 }.

2. 当n为偶数时, 采用二分求解, 假设p种礼物是否满足分配:

假设: 第一个人得到礼物1~r1, 分配的策略可以是: 偶数编号的人尽量往前取, 奇数的人尽量往

后取. 这样需要第n个人在不冲突的条件下, 尽量可能的往后取rn样礼物. 最后判断编号1

和编号n的人是否冲突即可.

例: p = 8, r = {2,2,5,2,5}

第一人:{1,2},第二人:{3,4}, 第三人:{8,7,6,5,2}, 第四人:{1,3}, 第五人:{8,7,6,5,4}

#include<stdio.h>
#include<algorithm>
using namespace std;
int p[100005], left[100005], right[100005], n; //left:第i个人拿到的左边的礼物总数。right同
int check(int x) {//x个礼物,是否够分
	int a = p[1], b = x - p[1]; //向左取和向右取
	left[1] = a; right[1] = 0;
	for (int i = 2; i <= n; i++) {
		if (i % 2 != 0) {
			right[i] = min(b - right[i - 1], p[i]); //尽量拿右边的礼物
			left[i] = p[i] - right[i];
		}
		else {
			left[i] = min(a - left[i - 1], p[i]);//尽量拿左边的礼物
			right[i] = p[i] - left[i];
		}
	}
	return left[n] == 0;
}
int main() {
	while (scanf("%d", &n) == 1, n) {
		for (int i = 1; i <= n; i++) {
			scanf("%d", &p[i]);
		}
		int L = 0, R = 0;
		p[n + 1] = p[1];
		for (int i = 1; i <= n; i++) {
			L = max(L, p[i] + p[i + 1]);
			R += p[i];
		}
		if (n == 1) {//需要特判
			printf("%d\n", p[1]);
			continue;
		}
		if (n % 2 != 0) {
//			for (int i = 1; i <= n; i++) {
//				R = max(R, p[i] * 3);
//			}
			while (L < R) { //二分
				int mid = (L + R) / 2;
				if (check(mid)) R = mid;
				else L = mid + 1;
			}
		}
		printf("%d\n", L);
	}
	return 0;
}
时间: 2024-10-10 08:51:48

uva 1335 Beijing Guards(二分)的相关文章

【二分答案+贪心】UVa 1335 - 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 we

[UVa 1335]Beijing Guards

题解 拿到题,没什么头绪,我们模拟一下,很容易得出一个结论: 如果$n$为偶数,那么答案就是$max(r[i]+r[i+1])$.具体方案就是,如果$i$为奇数,那么取$[1,r[i]]$:若$i$为偶数,则取$[ans-r[i]+1,ans]$. 同样我们此时的$ans$是答案的下界. 可当$n$为奇数时,我们这样贪心策略出现问题就是由于$1$和$n$都是奇数,会取$[1,r[1]]$,$[1,r[n]]$显然会重复,那么奇数我们就不能这样做了. 我们由$n$为偶数的思想,我们还是让$1$号取

Uva 长城守卫——1335 - Beijing Guards

二分查找+一定的技巧 1 #include<iostream> 2 using namespace std; 3 4 const int maxn=100000+10; 5 int n,r[maxn],Left[maxn],Right[maxn];//因为不用计算方案,所以可以按[1-r[i]]和[r[i]+1~p]中各拿几个分,当时没想到这个用set类写了个超耗时间的~~~~(>_<)~~~~ 6 7 bool ok(int p) 8 { 9 int x=r[1],y=p-r[1

uva 1356 - Bridge(积分+二分)

题目链接:uva 1356 - Bridge 题目大意:在一座长度为B的桥上建若干个塔,塔的间距不能超过D,塔的高度为H,塔之间的绳索形成全等的抛物线.绳索的总长度为L.问在建最少塔的情况下,绳索的最下段离地面的高度. 解题思路:贪心的思想求出最少情况下建立的塔数. 二分高度,然后用积分求出两塔之间绳索的长度. C++ 积分 #include <cstdio> #include <cstring> #include <cmath> #include <algori

Live Archive 3177 3177 - Beijing Guards 【枚举】

3177 - Beijing Guards Time limit: 3.000 seconds 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

AYITACM2016省赛第三周M - 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 ?nally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads. The walls wer

UVALIVE 3177 Beijing Guards

Description 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.

UVA 12124 - Assemble(二分)

题目链接:点击打开链接 题意: 给n个组件来组装电脑, 每个组件有4个属性:种类.名称.价格.品质. 要求每种组件买一个, 求在不超过预算的情况下, 品质最低的品质尽量大. 思路:很显然, 二分最低品质, 然后判断是否可行, 属于二分找上界,在二分时有一个小技巧用来处理当区间相差1时的情况. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i

UVA - 12338 Anti-Rhyme Pairs 二分+hash

题目链接:https://vjudge.net/problem/UVA-12338 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls