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 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...
随机推荐
- JavaScript读书笔记(6)-Array RegExp
1.Array类型 ECMAScript数组的每一项可以保存任何类型的数据,数组的大小也可以动态调整: (1) 创建数组 第一种方式:Array构造函数 var colors=new Array(); ...
- 对你的 REST API 进行保护的正确办法
设计好一个美丽的 REST + JSON API 之后,怎样对你的 API 进行保护?在 Stormpath,我们花了 18 个月来寻找最佳实践.将其一一实践于 Stormpath API 中并分析其 ...
- MySQL 忘记密码解决办法
第一步: 关闭MySQL服务. 第二步: 打开DOS窗口,在里面输入安装MqSQL的目录本机为:C:\Program Files\MySQL\MySQL Server 5.6\bin 第三步: 在命 ...
- python 基础 6.2 raise 关键字使用
一. raise 关键字 raise 用来触发异常 语法如下: raise[Exception [,args [,traceback]]] 语句中Exception 是异常 ...
- myql 5.6 安装
环境: centos 6.5 192.168.9.28 4核4G 虚拟机 一. 安装编译源码所需要的工具和库 [root@localhost ~]# yum -y install gcc gcc- ...
- 【BZOJ1499】[NOI2005]瑰丽华尔兹 单调队列+DP
[BZOJ1499][NOI2005]瑰丽华尔兹 Description 你跳过华尔兹吗?当音乐响起,当你随着旋律滑动舞步,是不是有一种漫步仙境的惬意?众所周知,跳华尔兹时,最重要的是有好的音乐.但是 ...
- 几句话搞懂URI、URL、URN之间的关系
1.URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. 2.RL是uniform resource locator,统一资源定位器,它是一种具体 ...
- 九度OJ 1055:数组逆置 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7324 解决:3429 题目描述: 输入一个字符串,长度小于等于200,然后将数组逆置输出. 输入: 测试数据有多组,每组输入一个字符串. ...
- 小程序的生命周期 launchApp
https://developers.weixin.qq.com/miniprogram/dev/api/launchApp.html?search-key=生命周期 launchApp(OBJECT ...
- mybatis 运算符转义收录
在ibatis配置文件写SQL语句的时候对于一些比如“<”,">","<>","&"," ' &q ...