線性dp+倒序思想 Codeforces P859C Pie Rules

Pie Rules

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?


題目大意:

有一個長度爲n的序列,Alice和Bob在玩遊戲。Bob先手掌握決策權。

他們從左向右掃整個序列,在任意時刻,擁有決策權的人有如下兩個選擇:

  1. 將當前的數加到自己的得分中,並將決策權給對方,對方將獲得下一個數的決策權

  2. 將當前的數加到對方的得分中,並將決策權保留給自己,自己將獲得下一個數的決策權

假定他們都使用最優策略,求他們最後分別能獲得多少分。

如果是正向思考,很難找到一個確切的狀態表示方式,因爲當前狀態選擇會影響後面的先手關係和最值關係,難以維護;

考慮倒序維護,dp[i] 表示第 i 個位置先手可以獲得的最大利益,那麼轉移方程爲:

dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+a[i]);

sum[i] 表示後綴和;

如果當前位置 i 不取,那麼後面還是先手,直接等於dp[i+1];如果取了,那麼後面就是另一個人先手,dp[i] 等於後綴和 - 另一個人先手的最大價值 + 當前價值;

代碼:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=2100;
const int M=2000100;
const LL mod=1e9+7;
int a[N],dp[N],sum[N];
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int 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;
}

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