dp - HNU 13404 The Imp
The Imp #
Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13404&courseid=0
#
Mean:
n个物品,每个物品价值为v,价格为c,你只可以带一个物品离开。
有一个精灵,它可以施法让你购买后的物品价值变为0(未离开商店之前),精灵最多施k次法术。
你的目的是让自己获得最大收益,而小鬼的目的正好相反。
如果你和精灵都采用最优策略,最后你可以盈利多少?
analyse:
第一感觉是三维dp,然而三维肯定会超时超内存。
然后就是想怎样压缩状态。。。
想了想其实两维就够了,为什么呢?因为对于第i件物品,如果我不选,那么它这次施不施法是没有影响的。
dp[i][j]:判断到第i个物品,精灵施了j次魔法,我还能获得的最大收益。
状态转移方程:dp[i][j]=max(dp[i-1][j],min(dp[i-1][j-1]-c,v-c))
伪代码:
for_each i
{
if(select i)
{
for_each j
{
if(magic j time)
{
max(before i) - cost;
}
else
{
value-cost;
}
}
}
else
{
max(before i);
}
}
{
if(select i)
{
for_each j
{
if(magic j time)
{
max(before i) - cost;
}
else
{
value-cost;
}
}
}
else
{
max(before i);
}
}
Time complexity: O(N*K)
Source code:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-15-12.09
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL __int64
#define ULL unsigned long long
using namespace std;
const LL MAXN=200010;
struct node
{
LL v,c;
bool operator <(const node&a) const
{
return v>a.v;
}
} a[MAXN];
LL dp[MAXN][10];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
LL Cas;
scanf("%I64d",&Cas);
while(Cas--)
{
LL n,k;
scanf("%I64d %I64d",&n,&k);
for(LL i=1; i<=n; ++i)
{
scanf("%I64d %I64d",&a[i].v,&a[i].c);
}
sort(a+1,a+n+1);
LL ans=0,val;
memset(dp,0,sizeof dp);
for(LL i=1; i<=n; ++i)
{
val=a[i].v-a[i].c;
dp[i][0]=max(dp[i-1][0],val);
for(LL j=1; j<=k; ++j)
{
dp[i][j]=max(dp[i-1][j],min(dp[i-1][j-1]-a[i].c,val));
}
ans=max(ans,dp[i][k]);
}
printf("%I64d\n",ans);
}
return 0;
}
/*
*/
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-15-12.09
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL __int64
#define ULL unsigned long long
using namespace std;
const LL MAXN=200010;
struct node
{
LL v,c;
bool operator <(const node&a) const
{
return v>a.v;
}
} a[MAXN];
LL dp[MAXN][10];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
LL Cas;
scanf("%I64d",&Cas);
while(Cas--)
{
LL n,k;
scanf("%I64d %I64d",&n,&k);
for(LL i=1; i<=n; ++i)
{
scanf("%I64d %I64d",&a[i].v,&a[i].c);
}
sort(a+1,a+n+1);
LL ans=0,val;
memset(dp,0,sizeof dp);
for(LL i=1; i<=n; ++i)
{
val=a[i].v-a[i].c;
dp[i][0]=max(dp[i-1][0],val);
for(LL j=1; j<=k; ++j)
{
dp[i][j]=max(dp[i-1][j],min(dp[i-1][j-1]-a[i].c,val));
}
ans=max(ans,dp[i][k]);
}
printf("%I64d\n",ans);
}
return 0;
}
/*
*/
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/4733125.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2014-08-15 计算几何 --- 凸包 模板
2014-08-15 计算几何 : 凸包学习笔记 --- Graham 扫描法
2014-08-15 拓扑排序 --- hdu 4948 : Kingdom