uva 1335 Beijing Guards(思维+对答案二分)

uva 1335 Beijing Guards(贪心+思维)

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 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个人想要a[i]个礼物,然后要求每相邻的孩子不能有相同的礼物,问最少需要多少种礼物。

解题思路:

如果只有一个孩子~那好办,他要多少就需要多少种。
如果多了呢?

我们先考虑有偶数个的情况

在这里插入图片描述
我们只要想相邻两个和最大那俩就行,在上图中是 a ,b 只是后 c肯定小于a,只要把a中拿2个给c 就肯定不和b重复,d只要从b中拿3个肯定不和a重复,因为c是从a中拿的,d是从b中拿的,一定也不同,6个人 8个人……偶数个人都类似,都是相邻的和最大的就是需要的礼物数。

接下来考虑比较麻烦的奇数个的情况,
  • 我们考虑最多需要多少个礼物呢?想要礼物最多的那个人假设想要 k个,那max_num=n*k,就是所需要礼物的最大数,所需的礼物数肯定 <max_num
  • 最少需要多少个呢?就类比偶数个的情况,最大的相邻和。
  • 这时候我们得出了一个我们所需要礼物数的区间(min_num,max_num),然后我们要求的是少需要多少,我们可以假设有一个能检测x个礼物的时候是否能够满足需求的函数judge()。
  • 最笨的方法我们就是从min_num,开始测试,看能不能满足?不能就加一,一直加到能满足的时候就找到正确答案。
  • 但是我们想啊,这种方法太耗时间了啊,我们可以用二分查找啊,每次看中间那个行不行。
while(l<=r)   //e二分求最优解
            {
                m=(l+r)/2;
                if(judge(m))  //如果礼物够分,就再减少一个礼物
                    r=m-1;
                else   //如果礼物不够分,就增加礼物
                    l=m+1;
            }

接下来,也是最关键的问题来了,就是如何设计这个judge()函数。

我们把所有的礼物分成两堆。分别放在左边和右边记为x,y
我们引入两个数组 left[]和right[]. left[i]表示第i个人从左边也就是x里拿的个数,right[i]表示第i个人从右边y里拿的个数,我们规定人的编号是偶数的从左边拿,奇数从右边拿,当自己这边不够了才能从另一边拿。也就是left[i]+right[i]=a[i]
我们把左边这堆定义为x=a[1],就是第一个人需要拿的个数,然后另一堆y=p-x,这时候我们开始轮着拿,拿呀拿,最后一个人肯定是奇数,应该从右边拿,如果他需要从左边拿,就一定不行了,为什么呢,因为最后一个是和第一个相邻的,然而左边一共就x个,第一个人就拿了x个,你最后这个人只要拿了左边的,一定会和左边的有重复的。
那我们具体怎么实现这个拿的操作呢?先看代码

if(i%2==0)
        {
            left[i]=min(a[i],x-left[i-1]);//尽量从左边拿,判断上一个人留下的礼物是否够,如果不够再拿右边
            right[i]=a[i]-left[i];//记录在右边拿了几个礼物
        }
        else
        {
            right[i]=min(a[i],y-right[i-1]);//尽量从右边拿,判断目前礼物是否够
            left[i]=a[i]-right[i]; //记录还需从右边拿几个
        }

left[i]=min(a[i],x-left[i-1]); 只要x剩下的够,肯定left[i]=a[i]了,就全从左边拿,这时候right[i]就等于0了,如果剩下的x-left[i-1]不够呢?left[i]=x-left[i-1]把左边剩下的全部拿走,然后从右边拿一些,来凑够a[i]。同理拿右边的时候也是如此。

小结:

1.这个题用了一个很巧妙的思想,我们不知道怎么求这个答案,那就找出个范围了,然后用二分开始试,一直试到答案为止,当然,前提肯定是找最大的或者最小的答案啦。不然咋二分
2.还有就是拿礼物的时候代码设计的很巧妙。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
using namespace std;
int a[100010],left[100010],right[100010],n;
int judge(int p)
{
    int x,y,i;
    left[1]=a[1],right[1]=0;
    x=a[1],y=p-a[1];/
    for(i=2; i<=n; i++)
    {
        if(i%2==0)
        {
            left[i]=min(a[i],x-left[i-1]);//尽量从左边拿,判断上一个人留下的礼物是否够,如果不够再拿右边
            right[i]=a[i]-left[i];//记录在右边拿了几个礼物
        }
        else
        {
            right[i]=min(a[i],y-right[i-1]);//尽量从右边拿,判断目前礼物是否够
            left[i]=a[i]-right[i]; //记录还需从右边拿几个
        }
    }
    return left[n]==0;//判断最后一个人是否拿过左边的礼物
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i,l=0,r=0,m;
        for(i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            r=max(r,a[i]*3);
        }
        if(n==1)
        {
            printf("%d\n",a[1]);
            continue;
        }
        a[n+1]=a[1];//因为是环形,所以需要考虑最后一个和第一个的关系
        for(i=1; i<=n; i++)
            l=max(l,a[i]+a[i+1]);
        if(n%2==1)  
        {
            while(l<=r)   //e二分求最优解
            {
                m=(l+r)/2;
                if(judge(m))  //如果礼物够分,就再减少一个物礼
                    r=m-1;
                else   
                    l=m+1;
            }
            printf("%d\n",r+1);
        }
        else 
            printf("%d\n",l);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43179892/article/details/84882415
今日推荐