CF859C Pie Rules(dp)

CF859C Pie Rules
luogu链接

题目描述
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 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.

输出格式
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.

题意翻译

有一个长度为n的序列,Alice和Bob在玩游戏。Bob先手掌握决策权。

他们从左向右扫整个序列,在任意时刻,拥有决策权的人有如下两个选择:

将当前的数加到自己的得分中,并将决策权给对方,对方将获得下一个数的决策权

将当前的数加到对方的得分中,并将决策权保留给自己,自己将获得下一个数的决策权

假定他们都使用最优策略,求他们最后分别能获得多少分

输入输出样例
输入 #1
3
141 592 653
输出 #1
653 733
输入 #2
5
10 21 10 21 10
输出 #2
31 41

说明/提示
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.

Solution

与博弈相关的dp
Bob先手,我们考虑Bob的选择,若定义dp[i]为选到i物品时(1->i)先手获得的最大收益,此时选或者不选都会都后面所有的状态产生影响,具有后效性。
我们考虑逆向dp,令dp[i]为当前从i先手开始选(i->n),直到游戏结束获得的最大收益。
dp[i] = max(dp[i + 1],sum[i + 1] - dp[i + 1] + num[i]) (选与不选)
dp[i] = max(dp[i + 1],sum[i] - dp[i + 1]);
sum记录的是后缀。

代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int SZ = 50 + 10;
const int INF = 0x3f3f3f3f;

int n,dp[SZ],sum[SZ];

int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++ )
    scanf("%d",&sum[i]);
    for(int i = n - 1;i >= 1;i -- )
    sum[i] += sum[i + 1];
    dp[n] = sum[n];
    for(int i = n - 1;i >= 1;i -- )
    {
        dp[i] = max(dp[i + 1],sum[i] - dp[i + 1]);
    }
    printf("%d %d",sum[1] - dp[1],dp[1]);
    //system("pause");
    return 0;
}

2020.4.3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值