Problem 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

思路:
之前想了一种思路被验证是想偏了,后来看了正确的答案后发现和HDU的2059龟兔赛跑是同一类型的题目
这俩题耽误了能有两天的时间,不过也算让我对DP的认识又上了一个台阶,因为他们都反映了DP的本质:一个状态的最优值是通过枚举所有能够到达这个状态的状态的值所得到的,知道了这点后,再就具体问题具体分析就OK
下面有两段代码,前一段是是按照之前的思路做的,自己和AC的代码比对了很多组数据(故意挑了一些比较刁钻的数据)结果都是相同的,但不知道为什么还是WA,先放在这儿等以后有能力了再来解决,后面的那段代码就是AC的了。

 解法一:(WA但数据测试正确)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int T;
int number;
struct P{
__int64 v;
__int64 c;
}pearl[];
int exits[];
__int64 sum; int main()
{
scanf("%d",&T);
while(T--)
{
sum = ;
scanf("%d",&number);
for(int i = ;i <= number;i++){
scanf("%I64d%I64d",&pearl[i].c,&pearl[i].v);
exits[i] = ;
}
for(int i = ;i <= number;i++)
if(pearl[i-].c*pearl[i].v < (pearl[i-].c+)*pearl[i-].v){
pearl[i].c += pearl[i-].c;
exits[i-] = ;
}
for(int i = ;i <= number;i++)
if(exits[i]) sum += (pearl[i].c+)*pearl[i].v;
printf("%I64d\n",sum);
}
return ;
}

解法二(AC):

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x7fffffff
using namespace std; int min(int a,int b)
{
return a<b?a:b;
} int main()
{
int T;
int sum[];
int val[];
int dp[];
int num[];
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
sum[] = ;
for(int i = ;i <= n;i++) {
scanf("%d%d",&num[i],&val[i]);
sum[i] = sum[i-]+num[i];
}
for(int i = ;i <= n;i++)
dp[i] = INF;
sum[] = ;
dp[] = ;
for(int i = ;i <= n;i++)
for(int j = ;j < i;j++)
dp[i] = min(dp[i],dp[j]+(sum[i]-sum[j]+)*val[i]);
printf("%d\n",dp[n]);
}
return ;
}

HDU-1300(基础方程DP-遍历之前所有状态)的更多相关文章

  1. HDU 1300 Pearls (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题目大意:珠宝店有100种不同质量的珍珠,质量越高价钱越高,为了促进销售,每买一种类型的珍珠,要 ...

  2. hdu 1300 Pearls(dp)

    Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. HDU-1176(基础方程DP)

    Problem Description 都 说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉, 就掉落在他 ...

  4. D - Pearls HDU - 1300 斜率dp+二分

    D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...

  5. hdu 1300 Pearls

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 思路:用dp[i]表示前i种花费最低的情况,则有dp[i]=min(dp[i],dp[j+1]+(( ...

  6. HDU 6321 (状压dp)

    题目大意:: 为给你n个点(n<=10,nn<=10,n) 初始时没有边相连 然后有m个操作(m<=30000m<=30000) 每次可以添加一条边或删除一条边 允许有重边 要 ...

  7. 基础树形DP小结

    HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...

  8. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  9. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. jQuery实现多级手风琴树形下拉菜单(源码)

    前几天因为公司的菜单要调整,公司的UI框架是不支持的,所以就自己在网上找了一个下拉菜单,可以支持多级菜单数据的,菜单数据是从xml文件中配置后读取的,网上有许多这方面的例子感觉不是很好用,就打了个包贴 ...

  2. (二)Struts2 核心知识

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 get/set 自动获取/设置数据 action代码: ...

  3. iOS 跳转到应用所在的App Store市场

    代码入下 #import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @end ...

  4. MVC5学习笔记

    买了一本MVC5的书:ASP.NET MVC 5 高级编程(第5版).边学边记录一下 1.快速创建模型类,如:自动实现的属性 {get;set;} 输入“prop",按Tab两次,默认属性值 ...

  5. javascript入门学习笔记2

    JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 实例 var x // x 为 undefined var x = 6; // x 为数字 var x = "Bil ...

  6. 月薪10K必备--C#下拉框联动

                   下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗 ...

  7. Mysql 操作手册

    mysql操作手册 版本:5.6.16mysql linux安装基本步骤:#rpm -e --nodeps mysql-lib-5.1.*#rpm -ivh mysql-server#rpm -ivh ...

  8. 完全背包的变形POJ1252

    话说今天做背包做到有点累了,题目是英文的--而且还很长,我看了好久(弱爆了). 题目大概的意思就是,有六种硬币,之后,求用这六种硬币最小数目支付1到100美分的平均值,以及最小数目中的最大值. 很容易 ...

  9. 如何给html元素的onclick事件传递参数(即如何获取html标签的data-*属性)

    现在做的一个小系统为了达到领导所说的很炫的效果有用到Metro UI CSS,但是因为如何给每个磁贴(div标签)的click事件传递参数折腾了蛮久(偶是菜鸟),后来终于找到一个解决方案即通过data ...

  10. angularjs应用骨架(4)

    继续上一篇 继续了解angular其他内容. 与服务器交互 真正的应用需要和真实的服务器进行交互移动应用和新兴的Chrome桌面应用可能是例外.但是对于此外的所有应用来说,无论是想把数据持久化到云端还 ...