题目地址:http://vjudge.net/problem/UVALive-3177
奇数情况竟然是二分求解的,一点想不到
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=(b);++i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=100000+5;
int r[maxn],Left[maxn],Right[maxn],n;
bool Check(int p){
int x=r[1],y=p-r[1];
Left[1]=x,Right[1]=0;
REP(i,2,n){
if(i&1) {
Right[i]=min(r[i],y-Right[i-1]);
Left[i]=r[i]-Right[i];
}
else {
Left[i]=min(r[i],x-Left[i-1]);
Right[i]=r[i]-Left[i];
}
}
return Left[n]==0;
}
int main(int argc, char const *argv[])
{
while(scanf("%d",&n)==1&&n)
{
REP(i,1,n) scanf("%d",&r[i]);
if(n==1) {printf("%d\n", r[1]); continue;}
int L=0;
r[n+1]=r[1];
REP(i,1,n) L=max(L,r[i]+r[i+1]);
if(n&1){
int R=L*3;
while(L<R){
int mid=(L+R)>>1;
if(Check(mid)) R=mid;
else L=mid+1;
}
}
printf("%d\n", L);
}
return 0;
}