UVALive 3177 Beijing Guards

偶数情况很好判断 奇数的时候 想了二分 但没想出怎么判断比较快 就放弃了 结果看了题解 就是二分额。。。 其实从左到右判断时 不用记录上一个人都选择了哪些数 只要记录在第一个人的范围内取了多少个 不在第一范围内取了多少个 另外n=1时要特判

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#define scnaf scanf
#define cahr char
#define bug puts("bugbugbug");
#define LL long long
using namespace std;
typedef long long ll;
const int mod=1000000007;
const int maxn=1e5+100;
const int inf=1e9;
const int maxe=200000;
int a[maxn];
int n;
int lleft[maxn];
int rright[maxn];
int pd(int x)
{
    int maxleft=a[0];
    int maxright=x-a[0];
    if(maxright<0) return 0;
    lleft[0]=a[0];
    rright[0]=0;
    for(int i=1;i<n;i++)
     if(i%2==1){
        int V=maxleft-lleft[i-1];
        if(a[i]<=V){
        lleft[i]=a[i];
        rright[i]=0;
        }
        else  {
            lleft[i]=V;
            rright[i]=a[i]-V;
            if(rright[i]+rright[i-1]>maxright)return 0;
        }
    }
    else
    {
         int V=maxright-rright[i-1];
         if(a[i]<=V)
         {
             rright[i]=a[i];
             lleft[i]=0;
         }
         else
         {
             rright[i]=V;
             lleft[i]=a[i]-V;
             if(lleft[i]+lleft[i-1]>maxleft)return 0;
         }
    }
    if(lleft[n-1]==0)return 1;
    return 0;
}
int main()
{
    while(scnaf("%d",&n) && n)
    {
        int ans=0;
        for(int i=0;i<n;i++)
        {
            scnaf("%d",&a[i]);
           if(i)ans=max(ans,a[i]+a[i-1]);
        }
        if(n==1)
        {
            printf("%d\n",a[0]);
            continue;
        }
        ans=max(ans,a[0]+a[n-1]);
        if(n%2)
        {
            int l=0,r=maxn;
            while(r>l)
            {
                int mid=l+(r-l)/2;
                if(pd(mid))
                    r=mid;
                else l=mid+1;
            }
            ans=r;
        }
        printf("%d\n",ans);

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值