UVA 1335 Beijing Guards

题目:

M - Beijing Guards

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 1335

Description

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, i

f 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士兵所需要的奖章,但相邻士兵奖章类型的不能有任何相同的.求最小的奖章类型

题目思路:

1、涂颜色问题,不过是线性的.

2、最小答案是两个相邻的和的最大值 最大的答案是三个相邻的和的最大值

分析:如果小于最小答案那么最大的两个和无法安排 ,因为一个点的颜色只与旁边两个有关,那么最大是两边都不相同,那么答案必然是三个数的和.

3、如果是偶数那么答案就是相邻最大和  奇数取下面,偶数取上面.

分析:第一个和最后一个分别取最下面,和最上面,相互不干扰.

4、如果是奇数,那么就需要搜索答案了  在这里我们可以用二分搜索

分析:因为答案左边的必然不成立,答案右边的必然不成立.可采用二分

5、搜索优化 :因为题目并没有要求答案,所以我们可以采用压缩记录,将搜索的答案分成两组,分别为下面组和上面组.区分方法:将搜索的第一个数为下组,其他为上组的数.

6、最后判断最后一个是否与第一个重复了,即在下组也有数值.

7、v[i]=min(a[i],(down-v[i-1]));//  v为上组  vv为下

8、      vv[i]=a[i]-v[i];

 

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int v[110000],vv[110000];
int n;
int a[110000];
int solve(int qiu);
int main()
{
    while(scanf("%d",&n)&&n)
    {
        int ans;
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        if(n==1)//特殊情况
        {
            printf("%d\n",a[0]);
            continue;
        }
        a[n]=a[0];//形成环,否则求最大出错
        int one_sum=max(a[0],a[1]);
        int two=a[1]+a[0];
        int two_sum=two;//最小值
        for(int i=2; i<=n; i++)
        {
            one_sum=max(a[i],one_sum);
            two=a[i]+a[i-1];
            two_sum=max(two_sum,two);
        }
        if(n%2==0)//偶数
            ans=two_sum;
        else
        {
            int left=two_sum;
            int right=one_sum*3;//最大值  
            while(left<right)//二分搜索
            {
                //cout<<'!'<<left<<right<<endl;
                ans=(left+right)/2;
                if(solve(ans))
                    right=ans;
                else
                    left=ans+1;
            }
            ans=left;
        }
        printf("%d\n",ans);
    }
    return 0;
}
int solve(int qiu)
{
    int down=a[0],up=qiu-a[0];
    v[0]=a[0];//从1-a[0]放了多少
    vv[0]=0;//剩下放了多少
    for(int i=1; i<n; i++)//从的二个开始放
    {
        if(i%2==1)
        {
            v[i]=min(a[i],(down-v[i-1]));//先放v
            vv[i]=a[i]-v[i];
        }
        else
        {
            vv[i]=min(a[i],(up-vv[i-1]));//
            v[i]=a[i]-vv[i];
        }
        //cout<<v[i]<<'\t'<<vv[i]<<endl;
    }
    if(v[n-1])//判断最后一个与第一个成立不
        return 0;
    else
        return 1;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值