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比较好理解,关键是从英文转化成中文再转化成目标性的语句有困难。

题意:

  先给t组测试样例,再给出n组数据,代表n组珍珠质量等级数,前者代表珍珠所属的质量等级,后者代表该质量等级里面的每颗珍珠的价格,质量等级和珍珠的价格都是升序。第一组数据说的是如果我要买3颗珍珠,一个等级一个等级购买的话需要(100+10)*1+(100+10)*2 =330元,但是如果全部按第二等级来买,需要(202+10)*2=424元,所以最小花费是330元,以此类推。要求求出最少花费价格去购买所有数量的珍珠。

https://blog.csdn.net/SDUTyangkun/article/details/52225285  这里面讲的很详细了

自己的理解:

 //解该题的关键在于珍珠的类别(也就是质量)和数量是呈上升趋势的
//每一个状态都是从上一个状态推算来的,所以可以推出动态规划的表达式 #include<iostream>
#include<string.h>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; struct node
{
int num;
int p;
}a[]; int dp[],sum[];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int t,n;
cin>>t;
while(t--)
{
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
cin>>n;
sum[]=;
for(int i=;i<=n;i++)
{
cin>>a[i].num>>a[i].p;
sum[i]=sum[i-]+a[i].num;
} dp[]=;
for(int i=;i<=n;i++)
{
dp[i]=(a[i].num+)*a[i].p+dp[i-];
//需要加上dp[i-1]
//因为(a[i].num+10)*a[i].p是当前位置的珍珠的价格,还未进行优化之前的;
//需要再加上之前所有状态的
for(int j=;j<i;j++)
//注意一下,这里的j从0开始,不是从1
//因为是从上一个状态得来的,要是从1开始,在i=1的时候会产生错误
{
dp[i]=min(dp[i],dp[j]+(sum[i]-sum[j]+)*a[i].p);
}
}
cout<<dp[n]<<endl;
}
return ;
}

POJ-1260-Pearls-dp+理解题意的更多相关文章

  1. poj 1260 Pearls(dp)

    题目:http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们的单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 珍珠的替代必须是连续的,不能跳跃 ...

  2. POJ 1260 Pearls 简单dp

    1.POJ 1260 2.链接:http://poj.org/problem?id=1260 3.总结:不太懂dp,看了题解 http://www.cnblogs.com/lyy289065406/a ...

  3. (线性结构dp )POJ 1260 Pearls

    Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10558   Accepted: 5489 Descripti ...

  4. POJ 1260 Pearls (斜率DP)题解

    思路: 直接DP也能做,这里用斜率DP. dp[i] = min{ dp[j] + ( sum[i] - sum[j] + 10 )*pr[i]} ; k<j<i  =>  dp[j ...

  5. poj 1260 Pearls 斜率优化dp

    这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...

  6. POJ 1260 Pearls (动规)

    Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7210 Accepted: 3543 Description In ...

  7. POJ 1260 Pearls

    Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6670 Accepted: 3248 Description In ...

  8. POJ 1260:Pearls(DP)

    http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8 ...

  9. POJ 1404 I-Keyboard (DP)

    http://poj.org/problem?id=1404 题意 :手机上的要发短信的话,“我”字需要先按一下9键,再按3下6键,所以,现在想要重新布局每个键上的字母数,让最后的那个值最小,也就是说 ...

  10. poj 50道dp题

    1.poj  3267 题意:给你一个字符串,下面有若干单词,问字符串要变成由下面单词组成的字符串,至少要删除多少个字母...... 例如: 6 10 browndcodw cow milk whit ...

随机推荐

  1. laravel 关掉debug

    修改.env文件 APP_DEBUG=false 然后把Laravel服务重启一下

  2. sqlserver 获取实例上用户数据库的数据字典

    原理很简单:将获取数据字典信息(通过动态视图获取)存入到目标表(数据字典表)中即可. 本人自用实例 1)创建相关的字典表 use YWMonitor GO SET ANSI_NULLS ON GO S ...

  3. Linux环境上的图形化界面SVN客户端软件“RabbitVCS”

    RabbitVCS基本支持所有的Linux发行版本包括ubuntu.Debian.Fedora.Arch Linux.Gentoo.Mandriva.OpenSUSE.RHEL.CentOS 5等.其 ...

  4. Typora--终于找到一个能够解决将csdn文章同步到hexo的完美编辑器(解决csdn图片防盗链导致无法直接复制文章的问题)。

    文章目录 需求 背景 新宠 告诉我,我的名字叫什么?大声点我听不见~ 页面 神奇之处 看得见的优点 如何设置项目根目录 如何显示图片? 于是最终操作流程 个人博客:https://mmmmmm.me ...

  5. Allow Pin Swapping Using these Methods options

    Frm:http://techdocs.altium.com/display/ADOH/Pin,+Pair+and+Part+Swapping#Pin,PairandPartSwapping-Swap ...

  6. <router-link :to="...">

    一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串<router-link to=& ...

  7. SQLServer 安装失败可能的原因

    问题:安装的时候显示参数指定的目录无效 解决:你的安装盘使用了文件/文件夹压缩功能,去掉压缩属性即可! 建议不要轻易使用储存盘的压缩功能

  8. Git 学习(三)Git 创建版本库

    获取 Git 仓库 什么是 Git 仓库呢,仓库又名版本库,我们可以把他理解为一个文件夹.这个文件夹里的所有东西都需要被 Git 给管理起来,对立面每个文件的修改.编辑.删除都将被 Git 记录,以便 ...

  9. java执行spark查询hbase的jar包出现错误提示:ob aborted due to stage failure: Master removed our application: FAILED

    执行java调用scala 打包后的jar时候出现异常 /14 23:57:08 WARN TaskSchedulerImpl: Initial job has not accepted any re ...

  10. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...