ACM学习历程——POJ1260 Pearls(动态规划)
Description
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
test case starts with a line containing the number of categories c
(1<=c<=100). Then, c lines follow, each with two numbers ai and
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 这是一道dp题,关键在于想出状态转移方程。
对于n个,一开始最初的计算方式来计算的话,n个是独立的个体,而到最后最优的计算方式,是把某几个设为一类。也是就是相当于把n个插板来分成若干集合,其中的价格取集合中最大的。
这样的话,可以看成这个最终状态是分几步实现的。
从纵向看可以,考虑任意前i个的最优。
从横向来看,任意j,从第j个到第i个都有可能合并成一个集合。.
横向的每次生成对i的一个集合。
纵向形成所有的集合。
状态转移方程为:
dp[i] = min{dp[j] + (sum[i]-sum[j])*p[i]) (j < i); 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; int a[105], p[105];
int sum[105], dp[105]; int main()
{
//freopen("test.txt", "r", stdin);
int T, n;
scanf("%d", &T);
for (int times = 0; times < T; ++times)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &a[i], &p[i]);
}
sum[0] = 0;
dp[0] = 0;
for (int i = 1; i <= n; ++i)
{
sum[i] = sum[i-1] + a[i];
dp[i] = dp[i-1] + (a[i]+10)*p[i];
}
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < i; ++j)
{
dp[i] = min(
dp[i],
dp[j] + (sum[i]-sum[j]+10)*p[i]);
}
}
printf("%d\n", dp[n]);
}
return 0;
}
ACM学习历程——POJ1260 Pearls(动态规划)的更多相关文章
- ACM学习历程—HDU2476 String painter(动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- ACM学习历程—HDU5586 Sum(动态规划)(BestCoder Round #64 (div.2) 1002)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 题目大意就是把一段序列里面的数替换成f(x),然后让总和最大. 首先可以计算出初始的总和,以及每 ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...
- ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...
随机推荐
- cartographer Ubuntu16.04 ros环境配置
首先要正确安装 ROS ,然后第12步应注意,proto的版本是个关键容易出错. 1.添加ROS源http:/packages.ros.org/ros/ubuntu xenial main ( ...
- Java开启/关闭tomcat服务器
© 版权声明:本文为博主原创文章,转载请注明出处 通过java代码实现Tomcat的开启与关闭 1.项目结构 2.CallTomcat.java package com.calltomcat.test ...
- HDFS源码分析数据块之CorruptReplicasMap
CorruptReplicasMap用于存储文件系统中所有损坏数据块的信息.仅当它的所有副本损坏时一个数据块才被认定为损坏.当汇报数据块的副本时,我们隐藏所有损坏副本.一旦一个数据块被发现完好副本达到 ...
- mysql数据库常用语句系列
mysql查询某个字段长度 一般查询语句:SELECT `lcontent` FROM `caiji_ym_liuyan` 查询数据: 有些时候需要查询某个字段的长度为多少时候才显示数据: SQL ...
- 【SQLServer2008】之改变主键当为null时也不会报错,可以入数据库。
在SqlServer红框中设置主键,右键会有添加主键选项,并且设置不能为null. 当我们插入主键数据如果为null时,会插不进去,这时候我们需要修改一下,如下图: “标识规范”中选择“是”,就可以了 ...
- 17 nginx连接memcached
一:配置php扩展memcached wget http://memcached.googlecode.com/files/memcached-1.4.9.tar.gz # tar zvxf memc ...
- 一个中断服务子程序ISR
请看下面的程序(一个中断服务子程序ISR),请指出这段代码的错误.)[中国台湾某著名CPU生产公司2005年面试题] 答案:(1)ISR不能返回一个值.如果你不懂这个,那么是不会被雇用的.(2)ISR ...
- 升级webapi依赖的Newtonsoft.json的版本(转)
随着微软日渐重视开源社区的贡献,微软在自己的产品中往往也会集成开源的第三方库. 比如System.Net.Http.Foramatting.dll 就依赖于Newtonsoft.json v4.5. ...
- 九度OJ 1005:Graduate Admission (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5646 解决:1632 题目描述: It is said that in 2011, there are about 100 graduat ...
- HTML5+ Android打包证书
HBuilder默认App云端打包默认使用的是DCloud公用证书,其信息如下: MD5: 59:20:1C:F6:58:92:02:CB:2C:DA:B2:67:52:47:21:12 SHA1:B ...