Description

In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls in it. The Royal Pearl has its name because it delivers to the royal family of Pearlania. 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

The first line of the input contains the number of test cases. Each
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

For each test case a single line containing a single number: the lowest possible price needed to buy everything on the list.

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(动态规划)的更多相关文章

  1. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

  2. ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...

  3. ACM学习历程—Hihocoder 1290 Demo Day(动态规划)

    http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...

  4. ACM学习历程—HDU5586 Sum(动态规划)(BestCoder Round #64 (div.2) 1002)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 题目大意就是把一段序列里面的数替换成f(x),然后让总和最大. 首先可以计算出初始的总和,以及每 ...

  5. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  6. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  7. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  8. ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)

    题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...

  9. ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...

随机推荐

  1. JavaScript--基于对象的脚本语言学习笔记(一)

    1.两种嵌入js的方式    使用javascript前缀构建url:<a href="javascript:alert('执行JavaScript. .')">执行j ...

  2. coreos 安装

    一.挂载coreos 镜像 引导live-cd 配置初始化coreos 系统 网卡和密码 进入live版系统后呈现这个状态 #任意编辑一个.network 文件,文件名随意,该文件不存在需自己创建 s ...

  3. 【puppeteer+Node.js】学习

    总结了一下有关puppeteer的学习的网站,以后还会继续更新 puppeteer 介绍 Puppeteer是一个通过DevTools Protocol控制headless chromium的高级no ...

  4. java8笔记: sorted()之正序倒序

    java8笔记: sorted()之正序倒序 这篇文章将会讲解Java 8 Stream sorted()示例 下面代码以自然序排序一个list List<Person> listTem ...

  5. TP 上传excel

    <?php class ExcelAction extends Action{ public function read($filename,$encode='utf-8'){ vendor(' ...

  6. PowerBuilder -- 其他

    判断某键是否被按下 KeyDown ( keycode ) 继承问题 如果是 uf_1是函数呢   你在父类UO_1的uf_1里面 写了代码,只要在子类UO_2的uf_1写了代码,默认是覆盖(over ...

  7. JavaMelody tomcat应用监控

    1 下载相关jar包,maven地址 测试发现 1.57.0版本tomcat6工程登陆报错,改用版本 1.50.0是正常的 <dependency> <groupId>net. ...

  8. CString 成员函数用法大全(转)

    CString( );例:CString csStr; CString( const CString& stringSrc );例:CString csStr("ABCDEF中文12 ...

  9. linux程序设计——网络信息(第十五章)

    15.3    网络信息 当眼下为止,客户和server程序一直是吧地址和port号编译到它们自己的内部. 对于一个更通用的server和客户程序来说.能够通过网络信息函数来决定应该使用的地址和por ...

  10. 【BZOJ3771】Triple 生成函数+FFT

    [BZOJ3771]Triple Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...