【CodeForces 859C --- Pie Rules】思维+dp

【CodeForces 859C --- Pie Rules】思维+dp

题目来源:点击进入【CodeForces 859C — Pie Rules】

Description

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.

Sample Input

3
141 592 653

Sample Output

653 733

Note

In the first example, Bob takes the size 141 slice for himself and gives the decider token to Alice. Then Alice gives the size 592 slice to Bob and keeps the decider token for herself, so that she can then give the size 653 slice to herself.

解题思路:

由题意知每个人选择时都是以最佳情况来选,最佳情况是指他的选择能使后面吃的更多。所以决定最佳情况是来自后面。
那么我们可以从后到前遍历。并且通过dp记录他在第i位时后面最多能吃多少。
在选择时实际上只有两种情况:
1.他要吃当前哪个馅饼。
2.他不吃当前的,吃后面那个馅饼。
我们只需将两种情况的最大值,赋值给dp[i]即可。

当然,在选择过程中,我们不需要知道当前第i位是谁在选择,我们只需要算出第i位时他的最佳情况是后面能吃多少。

我们知道第一个吃的人是Bob,那么我们可以通过第1位最佳情况时能吃多少得到Bob能吃多少。那么总量减去Bob吃的就是Alice吃的。

AC代码(C++):

#include 
#include 
#include 
#include 
#include 
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 55;
int sum[MAXN],dp[MAXN],arr[MAXN];

int main()
{
    SIS;
    int n;
    cin >> n;
    for(int i=0;i<n;i++) cin >> arr[i];
    for(int i=n-1;i>=0;i--)
    {
        sum[i]=sum[i+1]+arr[i];
        dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+arr[i]);
    }
    cout << sum[0]-dp[0] << ' ' << dp[0] << endl;
    return 0;
}

你可能感兴趣的:(ACM,CodeForces859C,PieRules,dp,思维)