【DP】CF859C Pie Rules

https://www.luogu.org/problemnew/show/CF859C

Description

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

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

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

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

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

Input

第一行是一个整数\(n\)代表序列长度

第二行有\(n\)个用空格隔开的整数,代表这个序列

Output

输出一行两个用空格隔开的整数,代表Alice和Bob的最终得分

Hint

\(Forall:\)

\(0~\leq~n~\leq~50\)

若设序列为\(a\),则\(1~\leq~a_i~\leq~100000\)

Solution

傻逼数据范围给了50……看着题目想折半搜索想了半天,搜了下题解发现是\(O(n)\)的DP……那你给我50的范围是要干嘛啊emmmm

考虑正着dp,设\(f_i\)为前\(i\)个数的ans,于是发现并不能转移,因为填表转移时是对手和你一起决策,一个取max一个取min显然没法做。填表法并不能记录这个状态是先手的还是后手的,记录先后手也不能做。

于是考虑倒着做,设\(f_i\)为从\(i\)开始选一直选到\(n\),发现这样的决策是自己一个人做最优决策,转移到下一维的最大值即可。方程显然:

\[f_i~=~\max(f_{i+1}~,sum_{i}-f_{i+1}) \]

其中\(sum\)代表后缀和

Code

#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long

typedef long long int ll;

namespace IPT {
	const int L = 1000000;
	char buf[L], *front=buf, *end=buf;
	char GetChar() {
		if(front == end) {
			end = buf + fread(front = buf, 1, L, stdin);
			if(front == end) return -1;
		}
		return *(front++);
	}
}

template <typename T>
inline void qr(T &x) {
	rg char ch = IPT::GetChar(), lst = ' ';
	while((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
	while((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
	if(lst == '-') x = -x;
}

template <typename T>
inline void ReadDb(T &x) {
	rg char ch = IPT::GetChar(), lst = ' ';
	while((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
	while((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
	if(ch == '.') {
		ch = IPT::GetChar();
		double base = 1;
		while((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
	}
	if(lst == '-') x = -x;
}

namespace OPT {
	char buf[120];
}

template <typename T>
inline void qw(T x, const char aft, const bool pt) {
	if(x < 0) {x = -x, putchar('-');}
	rg int top=0;
	do {OPT::buf[++top] = x % 10 + '0';} while( x /= 10);
	while(top) putchar(OPT::buf[top--]);
	if(pt) putchar(aft);
}

const int maxn = 55;

int n;
int MU[maxn], sum[maxn], frog[maxn];

int main() {
	freopen("1.in","r",stdin);
	qr(n);
	for(rg int i = 1; i <= n; ++i) qr(MU[i]);
	for(rg int i = n; i; --i) sum[i] = sum[i+1] + MU[i];
	for(rg int i = n; i; --i) frog[i] = std::max(frog[i+1], sum[i] - frog[i+1]);
	qw(sum[1] - frog[1], ' ', true);qw(frog[1], '\n', true);
	return 0;
}

Summary

正着不能DP时,考虑反着做

posted @ 2018-11-27 16:59  一扶苏一  阅读(691)  评论(0编辑  收藏  举报