POJ 1260 Pearls (动规)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7210 | Accepted: 3543 |
Description
But it also produces bracelets and necklaces for ordinary people. Of course the quality of the pearls for these people is much lower then the quality of pearls for the royal family.In Pearlania pearls are separated into 100 different quality classes. A quality
class is identified by the price for one single pearl in that quality class. This price is unique for that quality class and the price is always higher then the price for a pearl in a lower quality class.
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.
Input
pi. The first of these numbers is the number of pearls ai needed in a class (1 <= ai <= 1000).
The second number is the price per pearl pi in that class (1 <= pi <= 1000). The qualities of the classes (and so the prices) are given in ascending order. All numbers in the input are integers.
Output
Sample Input
2
2
100 1
100 2
3
1 10
1 11
100 12
Sample Output
330
1344 题意解释: 有n个等级的珠宝,等级依次升高。等级越高价钱越高,每买一个等级的不论什么数量的珠宝必须多付10颗此种珠宝的价钱。能够用高等级的珠宝取代低等级的。问要买到若干规定的数量和等级珠宝的最少花费。 比如买5颗价值为10的、100颗价值为20的珠宝,有两种方案:一种为分别买两种等级的珠宝价钱为(5+10)*10+(100+10)*20 = 2350。还有一种是将等级低的(即价格低)的珠宝全部换为等级高的,此时价钱为(5+100+10)*20 = 2300,故另外一种方案较优。 解题思路: 首先须要证明一点,用高一等级的取代低一等级的必须是连续取代。比如 3 5。90 7。100 12要想取代3个5价值的首先考虑用7价值的取代,由于3*7=21 (3+10)*5=65 显然比較合算,这样就不须要考虑用12价值的那个取代了。由于前面已经有了最优解。也就是说相互取代不会跳跃。 证明这一点后開始分析。用sum[i]表示前i中珠宝总数,对于第i等级珠宝,dp[i]表示其最优解,那么dp[i]可能是(sum[i]+10) *p[i] 即前面全部的珠宝用i的价值去买。或者(sum[i]-sum[1]+10)*p[i]+dp[1] 即从第二个開始前面的全部珠宝用i的价值去买,或者(sum[i]-sum[2] +10)*p[i] + dp[2] 即从第三个。。。依次计算。当中最小的一个就是i的解。注意,上面提到的跳跃是指不用i+1等级珠价值去计算i等级以及其下面的珠宝。但能够将i下面等级的珠宝归为等级为i的价格进行计算,这样就会保证当前价值最小。这样状态方程便有dp[i]=min(dp[i],(sum[i]-sum[j]+10)*p[i]+dp[j]),(1<=j<=i)。 在写状态方程时是总体到局部的思路去考虑。详细写代码特别是边界条件时则须要自底下向上推导。 通常须要将状态数组(此题为dp[])分开处理,即先对其边界赋值。处理特殊情况,再按状态方程进行计算。#include <stdio.h>
#include <string.h>
int tcase, n, a[2000], p[2000], dp[2000], sum[2000]; int min (int x, int y)
{
return x>y?y:x;
} int main ()
{
int i, j;
scanf("%d", &tcase);
while(tcase--)
{
memset(dp, 0, sizeof(dp));
sum[0] = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%d%d", &a[i], &p[i]);
sum[i] = sum[i-1] + a[i];
dp[i] = (sum[i]+10) * p[i];
} // 数组临界预处理。 for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
dp[i] = min (dp[i], (sum[i]-sum[j]+10)*p[i] + dp[j]);
// 依据状态转移方程计算数组。
// 此处取最小值的方法较巧妙,值得学习。
printf("%d\n", dp[n]);
}
return 0;
}
POJ 1260 Pearls (动规)的更多相关文章
- POJ 1260 Pearls 简单dp
1.POJ 1260 2.链接:http://poj.org/problem?id=1260 3.总结:不太懂dp,看了题解 http://www.cnblogs.com/lyy289065406/a ...
- poj 1260 Pearls(dp)
题目:http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们的单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 珍珠的替代必须是连续的,不能跳跃 ...
- POJ 1260 Pearls
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6670 Accepted: 3248 Description In ...
- (线性结构dp )POJ 1260 Pearls
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10558 Accepted: 5489 Descripti ...
- POJ 1088 滑雪 (动规)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 75664 Accepted: 28044 Description Mich ...
- POJ 1260 Pearls (斜率DP)题解
思路: 直接DP也能做,这里用斜率DP. dp[i] = min{ dp[j] + ( sum[i] - sum[j] + 10 )*pr[i]} ; k<j<i => dp[j ...
- HDU 1260 Tickets (动规)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- poj 1260 Pearls 斜率优化dp
这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...
- POJ 1260:Pearls(DP)
http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8 ...
随机推荐
- (29)zabbix执行远程命令
概述 监控,有的人只把他当做报警使用,出现问题之后打开跑回家打开电脑,巴拉巴拉的处理掉,大多数时候都是一些小问题,为何不让zabbix帮你把这些事情处理掉呢?和朋友具体,收到xx硬盘空间慢了.xx服务 ...
- Linux中文件压缩与解压
压缩与解压 compress 文件名 1 -v //详细信息 2 3 -d //等于 uncompress 默认只识别 .Z 如果使用别的后缀,会导致不识别,解压缩失败.也可以使用 -d -c 压缩包 ...
- docker参考文档
docker 使用笔记 http://www.cnblogs.com/xguo/p/3829329.html docker数据存储 | 单线程 http://opjasee.com/2014/06/2 ...
- datetime模块,random模块
6.10自我总结 1.datetime模块(用于修改日期) import datetime print(datetime.datetime.now(),type(datetime.datetime.n ...
- 数据结构( Pyhon 语言描述 ) — — 第1章:Python编程基础
变量和赋值语句 在同一条赋值语句中可以引入多个变量 交换变量a 和b 的值 a,b = b,a Python换行可以使用转义字符\,下一行的缩进量相同 )\ 帮助文档 help() 控制语句 条件式语 ...
- Python模块目录
阅读目录 模块 模块语法 常用模块 collections模块 time模块 random模块 os模块 sys模块 序列化模块 shelve模块 pickle模块 json模块 configpars ...
- Bin Paking Problem:简单的构造性算法
*本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 目录 BL和BLF算法 BF算法 HR算法 PH算法
- 【02】markdown在线编辑器
[01]在线编辑器 https://www.zybuluo.com/mdeditor 在线 Markdown 编辑阅读器 pen - 是一个Markdown编辑器工具.demo 你可以试试这个在线的m ...
- C#-dynamic参考
dynamic 类型的作用是绕过编译时类型检查,改为在运行时进行解析. dynamic 类型简化了对 COM API(例如 Office Automation API).动态 API(例如 IronP ...
- xmpp 常见错误 一
#pragma mark - 密码错误,身份验证失败 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement ...