【DP】CF859C Pie Rules
https://www.luogu.org/problemnew/show/CF859C
Description
有一个长度为的序列,Alice和Bob在玩游戏。Bob先手掌握决策权。
他们从左向右扫整个序列,在任意时刻,拥有决策权的人有如下两个选择:
将当前的数加到自己的得分中,并将决策权给对方,对方将获得下一个数的决策权
将当前的数加到对方的得分中,并将决策权保留给自己,自己将获得下一个数的决策权
假定他们都使用最优策略,求他们最后分别能获得多少分
Input
第一行是一个整数代表序列长度
第二行有个用空格隔开的整数,代表这个序列
Output
输出一行两个用空格隔开的整数,代表Alice和Bob的最终得分
Hint
。
若设序列为,则
Solution
傻逼数据范围给了50……看着题目想折半搜索想了半天,搜了下题解发现是的DP……那你给我50的范围是要干嘛啊emmmm
考虑正着dp,设为前个数的ans,于是发现并不能转移,因为填表转移时是对手和你一起决策,一个取max一个取min显然没法做。填表法并不能记录这个状态是先手的还是后手的,记录先后手也不能做。
于是考虑倒着做,设为从开始选一直选到,发现这样的决策是自己一个人做最优决策,转移到下一维的最大值即可。方程显然:
其中代表后缀和
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时,考虑反着做
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具