uva1335 Beijing Guards

来源:互联网 发布:java web 图书馆管理 编辑:程序博客网 时间:2024/06/10 06:00

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

如果n是偶数,那么直接取相邻两个差的最大值,然后奇数尽量往前取,偶数尽量往后取即可。
如果n是奇数的话,1尽量往前取,n就应该尽量往后取,照这样的话1以后的都应该偶数往前取,奇数往后取。但是往后取的时候并不知道应该取到哪,所以需要先二分答案。然后贪心地取一遍验证就行了。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,r[100010],f[100010],g[100010];int solve0(){    int i,ret=0;    r[n+1]=r[1];    for (i=1;i<=n;i++)      ret=max(ret,r[i]+r[i+1]);    return ret;}bool ok(int x){    int i;    f[1]=r[1];    g[1]=0;    for (i=2;i<=n;i++)    {        if (f[i-1]+g[i-1]+r[i]>x) return 0;        if (i&1)        {            g[i]=min(x-r[1]-g[i-1],r[i]);            f[i]=r[i]-g[i];        }        else        {            f[i]=min(r[1]-f[i-1],r[i]);            g[i]=r[i]-f[i];        }    }    return !f[n];}int solve1(){    if (n==1) return r[1];    int l=0,r=300000,mid;    while (l<r)    {        mid=(l+r)/2;        if (ok(mid)) r=mid;        else l=mid+1;    }    return l;}int main(){    int i;    while (scanf("%d",&n)&&n)    {        for (i=1;i<=n;i++)          scanf("%d",&r[i]);        printf("%d\n",(n&1)?solve1():solve0());    }}
0 0
原创粉丝点击