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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章