Division
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 216 Accepted Submission(s): 73
One day Lcy give them a lot of jewel. Each jewel has own value. They put the jewels in a line, and don’t swap the position of jewel. In order to division the jewels, they think a lot of ideas. At last, they decide a way to division by voting. First let Ctw to divide the line into two non-empty sublines (each subline express a group of jewel). Second yifenfei will to divide one of sublines and also divide it into two non-empty sublines. Than let lemon to choose one of this three sublines., than yifenfei choose one of remaining subline. At last Ctw hold the last subline..
Every one wants to maximize their own share. But because yifenfei don’t like Ctw, so if there are two divisions let himself get the some value, he will choose the way let Ctw get smaller values.
Assuming every one have full knowledge of each other’s strategies and make their decision optimally.
Each test case include, first one integers n. (3<=n<=100), express how many jewels.
Next one line include n integers Vi (Vi<2^31). The order of jewel is according the position in the line..
2
AC代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; ll a[123] , ans , sum[123]; struct node { ll a,b; }Max[123]; bool cmp(node x , node y) { if(x.b == y.b) return x.a < y.a; return x.b > y.b; } int main() { int n; while(~scanf("%d" , &n)) { sum[0] = 0; for(int i = 1 ; i <= n ; i++) { scanf("%lld" , &a[i]); sum[i] = sum[i - 1] + a[i]; } /*for(int i=1;i<=n;i++) printf("%d ",sum[i]); printf("\n");*/ ans = 0; ll num[4]; for(int i = 1 ; i < n ; i++) { int l1 = 1 , r1 = i; if(i == 1) { int cnt = 0; for(int j = i + 1 ; j < n ; j++) { num[0] = a[l1] , num[1] = sum[j] - sum[l1] , num[2] = sum[n] - sum[j]; sort(num , num + 3); Max[cnt].a = num[0] , Max[cnt++].b = num[1]; } if(cnt) { sort(Max , Max + cnt , cmp); ans = max(ans , Max[0].a); } } else if(i > 1 && i < n - 1) { int cnt = 0; for(int j = 1 ; j < i ; j++) { num[0] = sum[j] , num[1] = sum[r1] - sum[j] , num[2] = sum[n] - sum[r1]; sort(num , num + 3); Max[cnt].a = num[0] , Max[cnt++].b = num[1]; } for(int j = i + 1 ; j < n ; j++) { num[0] = sum[r1] , num[1] = sum[j] - sum[r1] , num[2] = sum[n] - sum[j]; sort(num , num + 3); Max[cnt].a= num[0] , Max[cnt++].b = num[1]; } if(cnt) { sort(Max , Max + cnt , cmp); ans = max(ans , Max[0].a); } } else { int cnt = 0; for(int j = 1 ; j < i ; j++) { num[0] = a[n] , num[1] = sum[j] , num[2] = sum[r1] - sum[j]; sort(num , num + 3); Max[cnt].a = num[0] , Max[cnt++].b = num[1]; } if(cnt) { sort(Max , Max + cnt , cmp); ans = max(ans , Max[0].a); } } } printf("%lld\n" , ans); } return 0; }
AC代码2:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int n;
ll a[100+7],Tsum[(100+7)<<2];
struct node
{
ll a,b;
}Max[123];
bool cmp(node x , node y)
{
if(x.b == y.b)
return x.a < y.a;
return x.b > y.b;
}
void pushup(int id)
{
Tsum[id] = Tsum[id<<1] + Tsum[id<<1|1];
}
void build(int id,int l,int r)
{
if(l == r)
Tsum[id] = a[l];
else
{
int mid = (l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
pushup(id);
}
}
ll qury(int id,int l,int r,int L,int R)
{
if(L <= l && r <= R)
return Tsum[id];
else
{
ll re = 0;
int mid = (l+r) >> 1;
if(L <= mid)
re += qury(id<<1,l,mid,L,R);
if(R > mid)
re += qury(id<<1|1,mid+1,r,L,R);
return re;
}
}
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i = 1;i <= n;++i)
scanf("%lld",&a[i]);
memset(Tsum,0,sizeof(Tsum));
build(1,1,n);
ll all = qury(1,1,n,1,n);
//printf("%lld\n",all);
ll num[4];
ll ans=0;
for(int i=1;i<=n-1;i++)
{
if(i==1)
{
int cnt=0;
for(int j=i+1;j<n;j++)
{
num[0] = qury(1,1,n,1,1);
num[1] = qury(1,1,n,i+1,j);
num[2]= all - num[0]-num[1];
sort(num , num + 3);
Max[cnt].a = num[0] , Max[cnt++].b = num[1];
}
if(cnt)
{
sort(Max , Max + cnt , cmp);
ans = max(ans , Max[0].a);
}
}
else if(i>1&&i<n-1)
{
int cnt=0;
for(int j=1;j<i;j++)
{
num[0] = qury(1,1,n,1,j);
num[1]= qury(1,1,n,j+1,i);
num[2] = all - num[0]-num[1];
sort(num , num + 3);
Max[cnt].a = num[0] , Max[cnt++].b = num[1];
}
for(int j=i+1;j<n;j++)
{
num[0]= qury(1,1,n,1,i);
num[1]= qury(1,1,n,i+1,j);
num[2] = all - num[0]-num[1];
sort(num , num + 3);
Max[cnt].a = num[0] , Max[cnt++].b = num[1];
}
if(cnt)
{
sort(Max , Max + cnt , cmp);
ans = max(ans , Max[0].a);
}
}
else if(i==n-1)
{
int cnt=0;
for(int j=1;j<i;j++)
{
num[0] = qury(1,1,n,1,j);
num[1] = qury(1,1,n,j+1,i);
num[2] = all - num[0]-num[1];
sort(num , num + 3);
Max[cnt].a = num[0] , Max[cnt++].b = num[1];
}
if(cnt)
{
sort(Max , Max + cnt , cmp);
ans = max(ans , Max[0].a);
}
}
}
printf("%lld\n",ans);
}
return 0;
}