HDU-1300(基础方程DP-遍历之前所有状态)
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.
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.
2
100 1
100 2
3
1 10
1 11
100 12
思路:
解法一:(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-遍历之前所有状态)的更多相关文章
- HDU 1300 Pearls (DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题目大意:珠宝店有100种不同质量的珍珠,质量越高价钱越高,为了促进销售,每买一种类型的珍珠,要 ...
- hdu 1300 Pearls(dp)
Pearls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU-1176(基础方程DP)
Problem Description 都 说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉, 就掉落在他 ...
- D - Pearls HDU - 1300 斜率dp+二分
D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...
- hdu 1300 Pearls
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 思路:用dp[i]表示前i种花费最低的情况,则有dp[i]=min(dp[i],dp[j+1]+(( ...
- HDU 6321 (状压dp)
题目大意:: 为给你n个点(n<=10,nn<=10,n) 初始时没有边相连 然后有m个操作(m<=30000m<=30000) 每次可以添加一条边或删除一条边 允许有重边 要 ...
- 基础树形DP小结
HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- Access的转义字符
Access中数据库转义字符规则: 插入.更新.=匹配 数据时,文本类型如用''括起来,中间可以有 ",*,%,[,],/,/,?,(,),{,}的任意组合,如要插入一个',需写''并在整个 ...
- CSS3 文本装饰
浏览器对CSS3文本特性的支持情况,如下表所示: 浏览器 text-shadow text-overflow word-wrap hyphens Opera 9.5+ 9+.带前缀-o- 10.5+ ...
- Python:字典
#!/usr/bin/python3 #dict 字典 #字典是可变的 dict1 = {"name":"张三","age":22} pri ...
- UVA10142/PC110108Australian Voting
UVA10142/PC110108Australian Voting 10142 Australian Voting Accepted C++11 0.769 2014-02-11 05:01:20 ...
- js中的prototye
前言 没事的时候写着js完,一般可能大家都知道这个属性吧,但是我还要说说,给一些不知道的人看看吧, 希望对你有帮助. 过程 以前在学c#的时候,老师最多用的就是Person这个类来开讲,我觉得是这个更 ...
- yii下多条件多表组合查询以及自写ajax分页
多条件组合查询主要用到yii的CDbCriteria,这个类很多oem框架都有,非常好用. 前台表单 前台查询表单效果是这样的,多个条件组,每个组里放多个input,name为数组.当任何一个复选框被 ...
- 求LR(0)文法的规范族集和ACTION表、GOTO表的构造算法
原理 数据结构 // GO private static Map<Map<Integer,String>,Integer> GO = new HashMap<Map< ...
- Unity3D C#脚本开发学习
1. Inherit from MonoBehaviour,All behaviour scripts must inherit from MonoBehaviour (directly or ind ...
- css3 3D盒子效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 613 Solved: 256[Submit][Status] ...