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

这篇博客探讨了CodeForces中859C问题,即如何公平地分配一系列派饼。Alice和Bob通过轮流决定派饼归属来分配,目标是最大化各自吃到的派饼总质量。博客介绍了最优策略和动态规划求解方法,通过dp数组计算在不同情况下每个人能得到的最大派饼质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值