CodeForces - 859C. Pie Rules(思维+dp)

You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be eaten by just one person.

The way Alice and Bob decide who eats each slice is as follows. First, the order in which the pies are to be handed out is decided. There is a special token called the "decider" token, initially held by Bob. Until all the pie is handed out, whoever has the decider token will give the next slice of pie to one of the participants, and the decider token to the other participant. They continue until no slices of pie are left.

All of the slices are of excellent quality, so each participant obviously wants to maximize the total amount of pie they get to eat. Assuming both players make their decisions optimally, how much pie will each participant receive?

Input

Input will begin with an integer N (1 ≤ N ≤ 50), the number of slices of pie. 

Following this is a line with N integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out.

Output

Print two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally.

思路:设dp[i]表示  第i位握有令牌的人的最大值。由于他们都想让各自最优,那么不论谁握有令牌,第i位握有令牌的最大值(dp[i])是不变的。

但是由于只知道,最开始Bob握有令牌,那么dp[1]一定表示的是第1位Bob握有令牌的最大值。其他位置并不知道谁握有令牌,则需要从后往前推,最后输出dp[1]。

方程为:dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+a[i]);(不要,要)若dp[i]要a[i],那么他的下一状态一定是不握令牌的即:sum[i+1]-dp[i+1]

# include <iostream>
# include <cstdio>
# include <cmath>
#define ll long long
# define pi acos(-1.0)
using namespace std;
ll a[1008611],dp[1008611],sum[1008611];
int main(){
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
    }
    for(ll i=n;i>=1;i--){
        sum[i]=sum[i+1]+a[i];
        dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+a[i]);
    }
    cout<<sum[1]-dp[1]<<' '<<dp[1]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao_you_you/article/details/89360652
pie