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 ...
随机推荐
- 基于restframework进行token验证
一般情况下,进入到web网站主页都需要进行token或者其它验证,不能在没有登录的情况下可以查看主页的内容,在用户输入用户名密码后,进行校验成功,后台会返回一个token,用于用于下次访问主页或其它页 ...
- CSIC_716_20191127【组合,封装、类的私有属性方法、property装饰器】
组合 what? 组合是指一个对象中,包含另一个或多个对象. why? 减少代码的冗余. How? 在类中加入其他类的对象,实现跨类对象之间的联动. 耦合度 软件设计要 高内聚 ...
- windows10,nodejs安装步骤
系统: windows10 1.下载: https://nodejs.org/en/ 2.下载最新版本,根据你的系统选择32位或者64位: 3.建议选择源码源码安装,不选择编译后的安装 如: 4.进行 ...
- python 打印出水仙花数
打印出三位水仙花数方法及解释 num = 100while num <= 999: #这里num 小于等于999 则运行 填1000也可以 a = num % 10 #num对10取余 b = ...
- Java ArrayList使用技巧 - 两个ArrayList去除重复的元素
方法一.ArrayList中提供的removeAll方法(效率最低) List1.removeAll(mSubList); 方法二.双重循环(比方法一效率高) 双重循环分为内外两层循环,经过测试,将元 ...
- 清除浏览器默认样式——css reset & normalize.css
css reset 自己挨个清除很麻烦 可以使用网上一些css库——css reset 把模板复制到css文件最上方,其他的样式我们自己编写来覆盖它们 但是这个也有一些弊端,会把一些本来需要的样式给清 ...
- thinkphp 动态配置
之前的方式都是通过预先定义配置文件的方式,而在具体的操作方法里面,我们仍然可以对某些参数进行动态配置(或者增加新的配置),主要是指那些还没有被使用的参数. 设置新的值: C('参数名称','新的参数值 ...
- 理解MITRE ATT&CK矩阵
最近准备学习一下关于ATT&CK的知识,这里面先来理解一下什么是ATT&CK(通过对ATT&CK的学习,可以很快的对安全领域有一个比较全面的认识). 什么是MITRE MITR ...
- 牛客多校第八场 G Gemstones 栈/贪心
题意: 对于一个序列,把可以把连着三个相同的字母拿走,问最多拿走多少组. 题解: 直接模拟栈,三个栈顶元素相同则答案+1,并弹出栈 #include<bits/stdc++.h> usin ...
- Java 并发总结(三)
锁优化及注意事项 有助于提高锁的性能 减小所持有时间:例如不要对方法直接加锁,而是在方法中对具体访问临界资源的代码加锁 减小锁粒度:如ConcurrentHashMap 用读写锁代替独占锁 锁分离:如 ...