• UVa1335 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 l$ \le$n$ \le$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
    很不错的贪心题目,题目是书上的例题。代码直接照着书上敲的。翻译和题解直接贴上来好了。反正是用来备忘的~~~

    例题16  长城守卫(Beijing Guards, CERC 2004, LA 3177

    有n个人围成一个圈,其中第i个人想要ri个不同的礼物。相邻的两个人可以聊天,炫耀自己的礼物。如果两个相邻的人拥有同一种礼物,则双方都会很不高兴。问:一共需要多少种礼物才能满足所有人的需要?假设每种礼物有无穷多个,不相邻的两个人不会一起聊天,所以即使拿到相同的礼物也没关系。

    比如,一共有5个人,每个人都要一个礼物,则至少要3种礼物。如果把这3种礼物编号为1, 2, 3,则5个人拿到的礼物应分别是:1,2,1,2,3。如果每个人要两个礼物,则至少要5种礼物,且5个人拿到的礼物集合应该是:{1,2},{3,4},{1,5},{2,3},{4,5}

    【输入格式】

    输入包含多组数据。每组数据的第一行为一个整数n(1≤n≤100 000);以下n行按照圈上的顺序描述每个人的需求,其中每行为一个整数ri(1≤ri≤100 000),表示第i个人想要ri个不同的礼物。输入结束标志为n=0

    【输出格式】

    对于每组数据,输出所需礼物的种类数。

    【分析】

    如果n为偶数,那么答案为相邻的两个人的r值之和的最大值,即p=max{ri+ri+1}i=1, 2, …, n),规定rn+1=r1。不难看出,这个数值是答案的下限,而且还可以构造出只用p种礼物的方案:对于一个编号为i的人,如果i为奇数,发编号为1~r的礼物ri;如果i为偶数,发礼物p-ri+1~p,请读者自己验证它是否符合要求。

    n为奇数的情况比较棘手,因为上述方法不再奏效。这个时候需要二分答案:假设已知共有p种礼物,该如何分配呢?设第1个人的礼物是1~r1,不难发现最优的分配策略一定是这样的:编号为偶数的人尽量往前取,编号为奇数的人尽量往后取。这样,编号为n的人在不冲突的前提下,尽可能地往后取了rn样东西,最后判定编号为1的人和编号为n的人是否冲突即可。比如,n = 5A = {2, 2, 5, 2, 5}p = 8时,则第1个人取{1, 2}, 2个人取{3, 4}, 3个人取{8, 7, 6, 5, 2}, 4个人取{1, 3}, 5个人取{8, 7, 6, 5, 4},由于第1个人与第5个人不冲突,所以p = 8是可行的。

    程序实现上,由于题目并不要求输出方案,因此,只需记录每个人在[1~r1]的范围内取了几个,在[r1+1~n]的范围里取了几个(在程序中分别用left[i]right[i]表示),最后判断出第n个人在[1~r1]里面是否有取东西即可。

     

    View Code
     1 #include<stdio.h>
     2 #define MAXN 100005
     3 int n, r[MAXN], left[MAXN], right[MAXN];
     4 int max(int a,int b)
     5 {
     6     return a>b?a:b;
     7 }
     8 int min(int a,int b)
     9 {
    10     return a<b?a:b;
    11 }
    12 int Check(int p)
    13 {
    14     int x=r[1], y=p-r[1],i;
    15     left[1]=x;
    16     right[1]=0;
    17     for(i =2; i<=n; i++)
    18     {
    19         if(i%2==1)
    20         {
    21             right[i]=min(y-right[i-1],r[i]);
    22             left[i]=r[i]-right[i];
    23         }
    24         else
    25         {
    26             left[i]=min(x-left[i-1],r[i]);
    27             right[i]=r[i]-left[i];
    28         }
    29     }
    30     return left[n]==0;
    31 }
    32 
    33 int main(void)
    34 {
    35     int i;
    36     while(scanf("%d",&n)==1&&n)
    37     {
    38         for(i=1; i<=n; i++) scanf("%d",&r[i]);
    39         if(n==1)
    40         {
    41             printf("%d\n",r[1]);
    42             continue;
    43         }
    44         r[n+1]=r[1];
    45         int L=0,R=0;
    46         for(i=1; i<=n; i++) L=max(L,r[i]+r[i+1]);
    47         if(n % 2==1)
    48         {
    49             for(i=1; i<=n; i++) R=max(R,r[i]*3);
    50             while(L < R)
    51             {
    52                 int M=L+(R-L)/2;
    53                 if(Check(M)) R=M;
    54                 else L=M+1;
    55             }
    56         }
    57         printf("%d\n", L);
    58     }
    59     return 0;
    60 }

     

     



  • 相关阅读:
    oracle各个版本的exp/imp兼容性
    AJAX开发 下载和安装ATF的步骤
    页面省略显示——web
    Oracle数据库中分区表的操作方法
    Hashtable的使用(2)
    RMAN之实战RMAN备份
    oracle_索引使用简介
    oracle中rollback的使用方法
    Hashtable的使用
    startup 出现的监听错误或者未初始化服务句柄【数据库归档问题】
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2984917.html
Copyright © 2020-2023  润新知