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. A题笔记(1)

    #include <stdlib.h> exit(); #include <stdlib.h> 是 exit(0) 必须的头文件 否则会出现 exit was not decl ...

  2. Python:对象

    #!/usr/bin/python3 #对象实例 class Person: num=200 def __init__(self,name,sex): self.name=name self.sex= ...

  3. Codevs 2549 自然数和分解

    2549 自然数和分解 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 传送门 题目描述 Description 把自然数N分解为若干个自然数之和,输出方案数. 输 ...

  4. 新建线程与UI线程间的通信

    现在用一个实例来演示一下自己的新建线程与UI线程间的通信. UI界面包含3个控件: 一个输入框,用来输入数字: 一个显示框,用来显示从2开始,到输入数字之间的所有质数: 一个按钮,点击后获取输入框输入 ...

  5. var 的用法

    var 的用法相当于定义一个变量为局部的,如果在函数内部用 var 定义一个变量,函数执行结果后,该变量就消失,如果在函数内部不用 var 声明,则变量是全局的,在函数外部也可以用该变量. var a ...

  6. 如何更有效学习php开源项目的源码

    一.先把源代码安装起来,结合它的文档和手册,熟悉其功能和它的应用方式. 二.浏览源代码的目录结构,了解各个目录的功能. 三.经过以上两步后相信你对这个开源的产品有了一个初步的了解了,那现在就开始分析它 ...

  7. 移动端消除click事件的延迟效果

    https://github.com/Plaputta/jquery.event.special.fastclick 用fastclick事件,类似zepto的tap事件,若想去除连点效果,可在外层显 ...

  8. 使用jQuery播放/暂停 HTML5视频

    文章来自:http://blog.okbase.net/jquery2000/archive/4485.html 我尝试用jQuery控制HTML5视频,两个视频分别在两个tab中,我希望点中tab后 ...

  9. IE6 Bug overflow:hidden失效

    下面就是我所收集或遇到的IE6 Bug之一:overflow:hidden失效 当父元素的直接子元素或者下级子元素的样式拥有position:relative属性时,父元素的overflow:hidd ...

  10. 基于TCP的NAT子网穿透实验

    不得不说,在国内IP紧缺的现状下,NAT发挥了无比巨大的作用:它以把IP和端口重新分配的方式,满足了广大人民群众上网的强烈需求.但是对于个人服务器以及在内网中基于网络的嵌入式设备,却是个比较尴尬的事情 ...