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. 给右键 添加dos命令

    reg add "HKEY_CURRENT_USER\Console" /v "ScreenBufferSize" /t REG_DWORD /d 655361 ...

  2. ubuntu亮度无法自动调节终极解决方案

    友情链接:IT狂人博客 这个问题纠结了我快两年,主要是自己懒,写了个脚本来调节亮度,不过还是稍显不便.近日兴起折腾了一番,终于找到问题根结了. There are many ways to contr ...

  3. 打印出不同顺序的字符串&单引号和双引号的差异

    发现一个很好玩的打印顺序 package com.liaojianya.chapter1; /** * This program demonstrates the string. * @author ...

  4. the c programming language 2-3

    #include<stdio.h> #define MAXLINELEN 1000 int power(int base,int n) { ; ; ;i<n;i++) answer= ...

  5. Apache .htaccess Rewrite解决问号匹配的写法

    如news.asp?id=123 需要把它定向到 news/123.html 这个用 RewriteRule 怎么写啊? RewriteRule ^news\.asp\?id=(\d+)$ news/ ...

  6. SNN--Second Name Node

    NameNode有这么几个配置: Property Description Suggested value dfs.name.dir Directory in NameNode’s local fil ...

  7. python随机数

    前提:需要导入random模块 >>>import random 1.random.random random.random()用于生成一个0到1的随机符小数: 0 <= n ...

  8. union 与struct的空间计算

    一.x86 总体上遵循两个原则: 整体空间----占用空间最大的成员(的类型)所占字节数的整数倍 对齐原则----内存按结构成员的先后顺序排列,当排到该成员变量时,其前面已摆放的空间大小必须是该成员类 ...

  9. 学习Swift -- 构造器(上)

    构造器(上) 构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.这个过程包含了为实例中的每个存储型属性设置初始值和为其执行必要的准备和初始化任务. 构造过程是通过定义构造器(Initi ...

  10. Learning LexRank——Graph-based Centrality as Salience in Text Summarization(一)

    (1)What is Sentence Centrality and Centroid-based Summarization ? Extractive summarization works by ...