Pie Rules CodeForces

来源:互联网 发布:福建网络电视台投诉 编辑:程序博客网 时间:2024/06/05 17:27

题目:

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.

Example
Input
3141 592 653
Output
653 733
Input
510 21 10 21 10
Output
31 41
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.


题意:

有一个长度为N的权值数组,两个人A,B。定义一种权力X为:决定当前这个数组元素分给谁。下次权利X授予没有分得前一个元素的人。如,现在A有权力,将当前数组元素分给自己,那么下一次权力X将被B获得。如果A将数组元素分给B,那么A继续拥有这个权利。一开始权利在A手中,每个人都会采取对自己最有利的方式分配。问最后两个人各自的权值总和。

题目分析:

一开始定义了一个老复杂的状态。。。。然后还猜出来转移方程了QAQ,果然是错的。。。。

正解其实很简单,但是很难想到==!定义dp[i]:到第i个为止,当前获得权力的人所得到的最大值。因为我们只知道第一个权力属于谁,所以我们最后答案肯定是dp[1],这决定了我们需要逆向推导。考虑第I个,dp[i],如果当前这个人要第I个,那么他可以由sum[i+1] - dp[i+1] + val[i]推出。如果他不要,那么可以由dp[i+1]直接推出。

代码

#include <iostream>#include <algorithm>using namespace std;int val[55],sum[55],dp[55];int main(){    int n;    cin>>n;    for(int i=1;i<=n;i++) cin>>val[i];    for(int i=n;i>=1;i--){        sum[i]=sum[i+1]+val[i];        dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+val[i]);    }    cout<<sum[1]-dp[1]<<" "<<dp[1]<<endl;    return 0;}

原创粉丝点击