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 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 l≤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.

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个人围成一个环,n与1,n-1相邻。每个人都需要一定个数的礼物,相邻两个人的礼物不能相同,并且每种礼物的数目都是无限的,问能够满足条件的最少的礼物的总数。

分析:如果n为偶数,那么就是相邻两个数和的最大值,如果是奇数的话,只能枚举,将礼物分成两部分,左和右,偶数号的尽量选左面的,奇数的尽量选右边的,最后只需要判断l[n]是不是等于0即可(因为刚开始第1个是选取最左面的,如果n和1有重复的,那么l[n]肯定不是0)。(参考 入门经典);

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 1e5+5;
using namespace std;

int l[maxn], r[maxn], s[maxn], n;

bool test(int p){
    int x = s[1], y = p - x;
    l[1] = s[1], r[1] = 0;  //根据第一个将p分成两部分, 因为第一个是选取的左边的,所以下一个尽量选右边的,这样最后一个就是尽量选右边的,如果l[n] == 0,就说明满足要求(很巧妙,值得思考)
    for(int i = 2; i <= n; ++ i){
        if(i % 2 == 0){
            l[i] = min(x-l[i-1], s[i]);
            r[i] = s[i]-l[i];
        }
        else {
            r[i] = min(y-r[i-1], s[i]);
            l[i] = s[i]-r[i];
        }
    }
    return l[n] == 0;
}

int main(){
    while(scanf("%d", &n), n){
        for(int i = 1; i <= n; ++ i) scanf("%d", &s[i]);
        if(n == 1) {
            printf("%d\n", s[1]); continue;
        }
        s[n+1] = s[1];
        int L = 0;
        for(int i = 1; i <= n; ++ i) L = max(L, s[i]+s[i+1]);
        if(n % 2 == 1){
            int R = L*3;
            //for(int i = 1; i <= n; ++ i ) R = max(R, s[i]*3);
            int m;
            while(L < R){
                m = L+(R-L)/2;
                if(test(m)) R = m; else L = m+1;
            }
        }
        printf("%d\n", L);
    }
    return 0;
} 
时间: 2024-12-25 07:07:58

Live Archive 3177 3177 - Beijing Guards 【枚举】的相关文章

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

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.

LA 3177 Beijing Guards(长城守卫)

贪心找最优策略.大白上有详解. 不过这里有几个值得思考的地方. 还有几个地方想不明白,脑子不够用了,先记着,改天再做一遍 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 100005; int n,a[maxn],left[maxn],right[maxn]; bool ok(int p) { int x = a[1],y = p-a[1]; left[1] = a[1];ri

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

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

[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

POJ3928 Ping pong

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their sk

hbx的毒瘤贪心系列题解

毒瘤hbx的贪心专题系列题解 A Maximal gcd 题意:现在给定一个正整数 n.你需要找到 k 个严格递增的正整数a1,?a2,?...,?ak,满足他们的和等于 n 并且他们的最大公因数尽量大.如果不可能请输出 -1.\(1\leq n,k \leq 10^{10}\) 题解:把 n 的所有因子找出来后,求最大因子 x 满足\(x* \frac {k* (k+1)}{2}\leq n\)即可.序列就是\(1* x,2* x,...,(k-1)* x,n-x* \frac{k* (k-1