HDU1300 Pearls —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-1300
Pearls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2920 Accepted Submission(s): 1464
Every month the stock manager of The Royal Pearl prepares a list with the number of pearls needed in each quality class. The pearls are bought on the local pearl market. Each quality class has its own price per pearl, but for every complete deal in a certain quality class one has to pay an extra amount of money equal to ten pearls in that class. This is to prevent tourists from buying just one pearl.
Also The Royal Pearl is suffering from the slow-down of the global economy. Therefore the company needs to be more efficient. The CFO (chief financial officer) has discovered that he can sometimes save money by buying pearls in a higher quality class than is actually needed. No customer will blame The Royal Pearl for putting better pearls in the bracelets, as long as the prices remain the same.
For example 5 pearls are needed in the 10 Euro category and 100 pearls are needed in the 20 Euro category. That will normally cost: (5+10)*10 + (100+10)*20 = 2350 Euro.
Buying all 105 pearls in the 20 Euro category only costs: (5+100+10)*20 = 2300 Euro.
The problem is that it requires a lot of computing work before the CFO knows how many pearls can best be bought in a higher quality class. You are asked to help The Royal Pearl with a computer program.
Given a list with the number of pearls and the price per pearl in different quality classes, give the lowest possible price needed to buy everything on the list. Pearls can be bought in the requested, or in a higher quality class, but not in a lower one.
2
100 1
100 2
3
1 10
1 11
100 12
1344
题意:
进销珍珠。有n种珍珠,每种珍珠都有其需求量和价格。但是供货商要求,每一种珍珠(不管数量多少)都要附加购买10个这种类型的珍珠,或者一种较便宜的珍珠当成是较贵的珍珠,售价也如较贵的珍珠,这样就可以省去购买10个较便宜的珍珠。问:怎样购买才能以最低的花费完成进货任务。
题解:
1.首先可以得到一个结论:如果一种较便宜的珍珠要归到较贵的珍珠,那么这个较贵的珍珠必须选为刚好比较便宜珍珠贵一点点的(贪心的策略,既然都要归到较贵的了,那肯定要最便宜的那种。)
2.把珍珠依照价格排序之后,其实就是一个区间的划分。可以直接O(n^2)枚举DP,也可以通过斜率进行优化:
O(n^2):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; int val[MAXN], sum[MAXN], dp[MAXN]; int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
sum[] = ;
for(int i = ; i<=n; i++)
scanf("%d%d", &sum[i],&val[i]), sum[i] += sum[i-]; dp[] = ;
for(int i = ; i<=n; i++)
{
dp[i] = (sum[i]+)*val[i];
for(int j = ; j<=i-; j++)
dp[i] = min(dp[i], dp[j]+(sum[i]-sum[j]+)*val[i]);
}
printf("%d\n", dp[n]);
}
}
斜率优化:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; int val[MAXN], sum[MAXN], dp[MAXN];
int q[MAXN], head, tail; int getUp(int i, int j)
{
return dp[i] - dp[j];
} int getDown(int i, int j)
{
return sum[i] - sum[j];
} int getDp(int i, int j)
{
return dp[j] + (sum[i]-sum[j]+)*val[i];
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
sum[] = ;
for(int i = ; i<=n; i++)
scanf("%d%d", &sum[i],&val[i]), sum[i] += sum[i-]; dp[] = ;
head = tail = ;
q[tail++] = ;
for(int i = ; i<=n; i++)
{
while(head+<tail && getUp(q[head+],q[head])<=getDown(q[head+],q[head])*val[i]) head++;
dp[i] = getDp(i, q[head]);
while(head+<tail && getUp(i, q[tail-])*getDown(q[tail-],q[tail-])<=
getUp(q[tail-],q[tail-])*getDown(i, q[tail-])) tail--;
q[tail++] = i;
}
printf("%d\n", dp[n]);
}
}
HDU1300 Pearls —— 斜率优化DP的更多相关文章
- poj 1260 Pearls 斜率优化dp
这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...
- HDOJ 1300 Pearls 斜率优化dp
原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题意: 题目太长了..自己看吧 题解: 看懂题目,就会发现这是个傻逼dp题,斜率优化一下就好 代 ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...
- bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...
- [BZOJ3156]防御准备(斜率优化DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
- BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP
1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...
- BZOJ 3156: 防御准备 斜率优化DP
3156: 防御准备 Description Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...
随机推荐
- Ubuntu 下使用 sshfs 挂载远程目录到本地
参考链接:http://blog.csdn.net/netwalk/article/details/12952719 一.Ubuntu 上安装sshfs Ubuntu源中已经包含了sshfs,以及所需 ...
- Bat命令学习 (转载)
基础部分:====================================================================== 一.基础语法: 1.批处理文件是一个“.bat” ...
- 51nod1053 最大M子段和 V2
$n \leq 50000$的序列,问选不超过$m \leq 50000$个区间使得和最大. 如果正数区间总数比$m$小那肯定全选.否则有两种方式减少区间数量:丢掉一个正区间:补一个负区间连接两个正区 ...
- raspi集成库及安装
原文:http://blog.csdn.net/xukai871105/article/details/12684617 树莓派来自国外,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层 ...
- SQL2000数据库密码被替换,重置密码提示未能找到存储过程sp_password解决方案
利用windows身份验证进入查询分析器后在master数据库下运行如下脚本: create procedure sp_password @old sysname = NULL, -- the old ...
- iOS数据持久化存储
本文中的代码托管在github上:https://github.com/WindyShade/DataSaveMethods 相对复杂的App仅靠内存的数据肯定无法满足,数据写磁盘作持久化存储是几乎每 ...
- 将一个文件从gbk编码转换为utf8编码
用django展示模板时,出现如下错误: 'utf8' codec can't decode byte 0xd3 in position 197: invalid continuation byte ...
- 零基础学python-3.2 变量赋值
这一节我们来具体展开变量赋值 1.以下我们举各种赋值的样例 anInt=12 anFloat=2.2 anStr='string' aList=['a','a','a'] anArray=(1,2,3 ...
- 【Android小项目】找不同,改编自"寻找房祖名"的一款开源小应用。
近期在微信朋友圈"寻找房祖名"和"万里寻刀"这类小游戏比較火.我试着写了一个android版本号的,里面全是一系列的形近字,实现原理非常easy:用一个Grid ...
- [WASM Rust] Use the js-sys Crate to Invoke Global APIs Available in Any JavaScript Environment
js-sys offers bindings to all the global APIs available in every JavaScript environment as defined b ...