zoukankan      html  css  js  c++  java
  • UVALIVE 3177 Beijing Guards

    Description

    Download as PDF
     

    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

    题意就不用说了
    解法:
    如果n是偶数,那么就直接是相邻两个和的最大值,奇数的情况就很复杂了,不过还是可以二分答案,然后判断可行性,第一个放1——a[1],其他的偶数位尽量往编号小的放,奇数伟尽量放编号大的,但是怎么处理呢!我们可以将答案ans看作 1——ans这个区间,对于每个数统计放在1——a[1]这个区间的个数,和a[1+1]——ans这个区间的个数,最后看看最后一个的左区间数是否为0;
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<ctime>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<sstream>
    #define inf 0x3f3f3f3f
    #define PI acos(-1.0)
    #define eps 1e-6
    #define LL long long
    #define MEM(a,b) memset(a,b,sizeof(a))
    #define PB push_back
    #define MP make_pair
    #define IN freopen("in.txt","r",stdin);
    #define OUT freopen("out.txt","w",stdout);
    #define BUG printf("bug************bug************bug
    ");
    
    using namespace std;
    const int maxn=100000+10;
    int a[maxn],n,l[maxn],r[maxn];
    
    bool if_find(int m)
    {
         l[1]=a[1]; r[1]=0;
         int LLL=a[1];
         int RRR=m-a[1];
         for (int i=2;i<=n;i++)
         {
              if (i%2==1)
              {
                   r[i]=min(RRR-r[i-1],a[i]);
                   l[i]=a[i]-r[i];
              }
              else
              {
                   l[i]=min(LLL-l[i-1],a[i]);
                   r[i]=a[i]-l[i];
              }
         }
         if (l[n]==0) return 1; else return 0;
    }
    
    int find(int x,int y)
    {
         while(x<y)
         {
              int m=x+(y-x)/2;
              if (if_find(m)) y=m;
              else x=m+1;
         }
         return x;
    }
    int main()
    {
         int L,R,ans;
         while(scanf("%d",&n)!=EOF && n)
         {
              L=0; R=0; ans=0;
              for (int i=1;i<=n;i++) {
                   scanf("%d",&a[i]);
                   if (i>0)ans=max(ans,a[i]+a[i-1]);
              }
              ans=max(ans,a[1]+a[n]);
              if (n%2==0) {printf("%d
    ",ans); continue; }
              if (n==1){printf("%d
    ",a[1]); continue; }
              L=ans; R=3*ans;
              ans=find(L,R);
              printf("%d
    ",ans);
         }
         return 0;
    }
    

      



  • 相关阅读:
    自然数e为底数的指数函数的一个小运用
    Windows产品测试集合整理
    随手写的 IniFiles
    Windows C++ TLS 实现连接163邮箱
    Windows创建个人证书(C++实现,使用 as administrator)
    单进程单线程IOCP的实现(含客户端和服务端)
    32/64位下面的基本数据类型的大小
    WMI 获取操作系统名称和版本
    http 基本代理 C++实现(极简)
    获取内存大小、CPU大小、硬盘大小及使用率
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3968948.html
Copyright © 2011-2022 走看看