POJ-1260-Pearls-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.
Input
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
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+理解题意的更多相关文章
- poj 1260 Pearls(dp)
题目:http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们的单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 珍珠的替代必须是连续的,不能跳跃 ...
- POJ 1260 Pearls 简单dp
1.POJ 1260 2.链接:http://poj.org/problem?id=1260 3.总结:不太懂dp,看了题解 http://www.cnblogs.com/lyy289065406/a ...
- (线性结构dp )POJ 1260 Pearls
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10558 Accepted: 5489 Descripti ...
- POJ 1260 Pearls (斜率DP)题解
思路: 直接DP也能做,这里用斜率DP. dp[i] = min{ dp[j] + ( sum[i] - sum[j] + 10 )*pr[i]} ; k<j<i => dp[j ...
- poj 1260 Pearls 斜率优化dp
这个题目数据量很小,但是满足斜率优化的条件,可以用斜率优化dp来做. 要注意的地方,0也是一个决策点. #include <iostream> #include <cstdio> ...
- POJ 1260 Pearls (动规)
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7210 Accepted: 3543 Description In ...
- POJ 1260 Pearls
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6670 Accepted: 3248 Description In ...
- POJ 1260:Pearls(DP)
http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8 ...
- POJ 1404 I-Keyboard (DP)
http://poj.org/problem?id=1404 题意 :手机上的要发短信的话,“我”字需要先按一下9键,再按3下6键,所以,现在想要重新布局每个键上的字母数,让最后的那个值最小,也就是说 ...
- poj 50道dp题
1.poj 3267 题意:给你一个字符串,下面有若干单词,问字符串要变成由下面单词组成的字符串,至少要删除多少个字母...... 例如: 6 10 browndcodw cow milk whit ...
随机推荐
- Mysql 编译报错 g++: internal compiler error: Killed (program cc1plus) 解决办法
g++: internal compiler error: Killed (program cc1plus) 解决办法 g++: internal compiler error: Killed (pr ...
- php fopen用法以及解释
$log = fopen('./log.txt','a');//生成一个log.txt文件,a相当于文件的权限 fwrite($log,'成功'."\r\n");//写入文件 mo ...
- Delphi 日期函数列表
引用单元 :DateUtils CompareDate 比较两个日期时间值日期部分的大小CompareDateTime 比较两个日期时间值的大小CompareTime 比较两个日期时间值时间部分的大小 ...
- PDO基础
//PDO:数据访问抽象层 $dsn = "mysql:dbname=mydb;host=localhost";//造PDO对象 $pdo = new PDO($dsn," ...
- js实现canvas保存图片为png格式并下载到本地
canvas 保存图片 下载到本地 function base64Img2Blob(code){ var parts = code.split(';base64,'); var contentType ...
- VS2010-MFC(Ribbon界面开发:为Ribbon Bar添加控件)
转自:http://www.jizhuomi.com/software/253.html 前面一节为大家简单介绍了如何创建Ribbon样式的应用程序框架,本节教程就来初步讲讲怎样为Ribbon Bar ...
- day 89 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件 本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...
- Comet OJ - 2019 六一欢乐赛
传送门 #A: 思路:等差数列求和,看成俩次1+2+…+ n,多加的n减去,所以 ans = n*(n+1) - n. AC代码: #include<iostream> #include& ...
- 【POJ】1251 Jungle Roads
题目链接:http://poj.org/problem?id=1251 题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离.求构建所有村庄道路的最短距离. 题解:最小生成树裸题.注意 ...
- 【牛客网多校第一场】A
题目链接:https://www.nowcoder.com/acm/contest/139/A 题意:大概就是给你0,1,2让你填矩阵问有多少种填法满足 a(i,j)<=a(i+1,j)以及a( ...