C. Pie Rules
C. Pie Rules
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
题意:A,B两个人玩一个游戏,开始时决定权属于B,每一轮拥有决定权的人,可以决定这一轮的面包给A还是给B ,没有得到面包的人有下一轮的决定权,每个人都希望自己得到的面包尽可能的多。
题解:考虑第i轮拥有决定权的人肯定会希望自己在i-n轮内获得的面包尽量 多,
设dp[i]表示第i轮拥有决定权的人在i-n轮内获得的最多面包量,这个人可以选择放弃这一轮的面包,拥有下一轮的决定权那么就是dp[i+1] ,或者选择这一轮的面包放弃权力也就是s[i+1]-dp[i+1]+c[i] --他得到的面包量就是 剩下的i+1-n的面包量减去 i+1轮拥有决定权的人在i+1-n轮可以拿到的面包量+第i轮得到的面包量。
dp[1]就表示B在第一轮有决定权,他在1-n中可以得到的最大面包量~~
QAQ 代码量少,但是没想法;;;
#include<bits/stdc++.h>
using namespace std;
int c[100];
int dp[100],s[100];
int main()
{
int n;cin>>n;s[n+1]=0;
for(int i=1;i<=n;i++)
{
cin>>c[i];
}
for(int i=n;i>=1;i--)
{
s[i]=s[i+1]+c[i];
dp[i]=max(dp[i+1],s[i+1]-dp[i+1]+c[i]);
}
cout<<s[1]-dp[1]<<" "<<dp[1]<<endl;
}