hdu2615

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/active2489595970/article/details/77067788

Division

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 216    Accepted Submission(s): 73


Problem Description
Yifenfei and lemon are good friends, they don’t like Ctw, because Ctw’s name is so worldliness.
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.
 

Input
The input contains multiple test cases.
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..
 

Output
For each test case output one integer that CTW is maximum value of jewels can get.
 

Sample Input
 
  
4 50 90 10 100 3 5 5 5 9 1 1 1 1 1 1 1 1 1
 

Sample Output
 
  
50 5

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;
}

猜你喜欢

转载自blog.csdn.net/active2489595970/article/details/77067788