hdu 1300 (dp)-程序员宅基地

技术标签: 动态规划  

Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1225    Accepted Submission(s): 550


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
 

Source
 

Recommend
Eddy
 
可以看出每种物品一定是在同种价格下一次买完的,并且如果我要以当前价格购买时,一定也会把前面所有积累的还没买的都在这种价格下买掉。dp[i][j]表示买完前i种物品,还剩余j个物品的最小花费,然后用从前往后推的方式,再加优先队列和一点剪枝,用这个bfs的方法也被我把这题水过了。这个方法理论上是不会超时的,因为可到达的状态实际上非常少。不过这题的更好方法是,dp[i]表示把前i种物品买完,dp[i]=min{dp[j]+value}(0<=j<i, value为第j+1类珠宝到第i类全部以i类买入的价值; );然后我们可以用一个数组记录一下0-i的花费。O(n^2)复杂度,还是好很多的。
贴上第一种做法的代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int,int> PP;
typedef pair<int,PP> P;
const int maxn = 100 + 5;
const int INF = 1000000000;

int num[maxn],p[maxn];
priority_queue<P> Q;
map<PP,int> M;

int bfs(int n){
    while(!Q.empty()){
        P s = Q.top();Q.pop();
        int price = -s.first;
        PP ss = s.second;
        int pos = ss.first;
        int sum = ss.second;
        M[PP(pos,sum)] = 1;
        if(pos == n && sum == 0) return price;
        if(pos < n){
            if(M[PP(pos+1,0)] == 0)
                Q.push(P(-(price+(10+sum+num[pos+1])*p[pos+1]),PP(pos+1,0)));
            if(M[PP(pos+1,sum+num[pos+1])] == 0)
                Q.push(P(-price,PP(pos+1,sum+num[pos+1])));
        }
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d%d",&num[i],&p[i]);
        }
        M.clear();
        while(!Q.empty()) Q.pop();
        Q.push(P(-(num[1]+10)*p[1],PP(1,0)));
        Q.push(P(0,PP(1,num[1])));
        int ans = bfs(n);
        printf("%d\n",ans);
    }
    return 0;
}



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Wiking__acm/article/details/12912389

智能推荐

SAP-PP MD04详解一_sap md04-程序员宅基地

文章浏览阅读6.2k次,点赞5次,收藏53次。MD04是SAP运行MRP后的结果查询事务码,其他功能强大,标准功能比如:查询/更改物料主数据、转换计划订单到生产订单/采购申请、查询/更改各个MRP元素对应的单据等等。而且还可以附加标准的菜单或者自定义添加事务码上去。老铁将做一个MD04详细说明系列,本篇为第一篇之MRP元素说明。 我们来看看MD04基本界面可以看到主要的数据显示界面,有日期、MRP元素、MRP元素数据、再计划日期、收货/需求、可用数量、工厂、库存地点等栏位。光标定位到MRP元素列,按F1即可看到各类MRP元素的说..._sap md04

Yolo-FastestV2: 更快,更轻,移动端可达300FPS,参数量仅250k-程序员宅基地

文章浏览阅读929次。作者丨qiuqiuqiu@知乎(已授权)来源 | https://zhuanlan.zhihu.com/p/400474142编辑 | AI约读社Yolo-FastestV2简单、快速、..._yolo fastestv2

测试入门:功能测试的流程梳理_功能验收测试过程中,通过()对测试用例执行过程及发现的缺陷进行管理-程序员宅基地

文章浏览阅读604次。进入公司后,如果项目分布范围比较广,一般会有测试分析师对不同项目做不同的测试策略。功能测试阶段需要的基础输入文档有:《需求规格说明书》、《产品概要设计》《产品详细设计》《设计文档》、设计图、项目代码、构建版本等。功能测试的输出物有:《测试内部草稿》(仅作为测试团队内部使用、成员协同编辑)《测试计划》《测试用例》《缺陷报告》《功能测试报告》《测试总结》《测试知识库》修订版(仅作为测试团队内部使用)等。其中,功能测试计划可以参考《一份标准的测试计划包含哪些要素》中的关键内容元素;测试计划完成后需要由不同角_功能验收测试过程中,通过()对测试用例执行过程及发现的缺陷进行管理

vscode新手注意事项(字体间隔,报错提示波浪线,头文件路径,opencv头文件路径)_vscode 插件里间距错误提醒是哪个-程序员宅基地

文章浏览阅读787次。vscode新手注意事项(字体间隔,报错提示波浪线,头文件路径,opencv头文件路径)一.字体空格刚安装vscode,不设置字体的话,字体间的间隔会很难受,需要进行如下配置。在 设置->首选项 选择 文本编辑器->字体 ,将“FONT Family ”选项修改成如下。Consolas,Consolas,monospace,Consolas二.报错提示波浪线vscode对代码进行错误提示,进行如下配置。(1)在设置->首选项 搜索errorsquiggles._vscode 插件里间距错误提醒是哪个

云服务器上划虚拟主机,云服务器上划虚拟主机-程序员宅基地

文章浏览阅读171次。云服务器上划虚拟主机 内容精选换一换您可以为需要容灾的云服务器在指定的保护组下创建保护实例。在当前的生产站点遇到不可抗力导致大规模服务器故障时,您可以调用保护组的操作接口进行故障切换,从而确保保护实例上运行的业务正常连续。为每一个需要复制的服务器挑选一个保护组,并创建一个保护实例。创建保护实例过程中,会在保护组的容灾站点创建对应的服务器和磁盘,服务器规格可根据需要进行选择,运行在专属主机和普通EC...

微型计算机故障分为哪几类,西南大学19秋[0240] 计算机维修技术在线作业-程序员宅基地

文章浏览阅读2.3k次。0240 计算机维修技术- M7 T, _$ E0 ef4 E1.[单选题]评定主板的性能首先要看()。. a4 k, f/ F% N0 C/ o4 d奥鹏作业答案可以联系QQ 7612960214 d' d: qk, l( H$ kA.C.CPU6 |, q" c! V# q1 R- i& eB.内存- z) K* M3 P: H2 {/ R0 bC.主板结构' n1 E2 ..._微型计算机常见有哪些故关型?并举例说明灰尘对微机设备会产生哪些故障关型与危害

随便推点

谈谈Tensorflow的dropout_对tensor dropout-程序员宅基地

文章浏览阅读498次。1.dropout是为了防止过拟合而使用的;Dropout这个概念已经推出4年了,它的详细描述见论文。可是呢,它仿佛是个犹抱琵琶半遮面的美女,难以捉摸!!许多文献都对dropout有过描述,但解释的含糊不清,这里呢,我也不打算解释清楚,只是通过tensorflow来看一看dropout的运行机理。文章分两部分,第一部分介绍tensorflow中的dropout函数,第二部分是我的..._对tensor dropout

0645-6.2.0-为什么我在CDH6上使用Spark2.4 Thrift失败了_spark sql nosuchmethoderror: org.apache.hadoop.hiv-程序员宅基地

文章浏览阅读1.6k次,点赞3次,收藏3次。Fayson的github: https://github.com/fayson/cdhproject推荐关注微信公众号:“Hadoop实操”,ID:gh_c4c535955d0f1 文档编写目的Fayson在前面的文章中介绍过什么是Spark Thrift,Spark Thrift的缺陷,以及Spark Thrift在CDH5中的使用情况,参考《0643-Spark SQL Thrif..._spark sql nosuchmethoderror: org.apache.hadoop.hive.ql.session.sessionstate$

C语言入门---构建素数表---翁恺MOOC_构建质数表的函数-程序员宅基地

文章浏览阅读1.3k次。(一个非素数x,一定可以表示成两个数(除了0和x本身以外)的乘积,这两个数必然有一个小于等于x的平方根,故可以使用 sqrt函数去求素数和)_构建质数表的函数

Ubuntu18.10下安装Qt5.12过程记录-程序员宅基地

文章浏览阅读310次。首先你得先安装Ubuntu操作系统(我是在VMWare14中安装的Ubuntu18.10版本)。阿里镜像:https://opsx.alibaba.com/mirror我这里下载的文件为:ubuntu-18.10-desktop-amd64.isoVMWare安装Ubuntu18.10过程省略…打开Ubuntu虚拟机,打开火狐浏览器,输入网址下载QT5.12(linux版本,约..._qt5 镜像 ubuntu18

android开发面试题_csdn android 移动软件开发 面试题-程序员宅基地

文章浏览阅读2.1k次。打包下载: Android面试题带答案.doc(108.5 KB, 下载次数: 2126) 2012-1-11 11:20 上传点击文件名下载附件 下载积分: 下载豆 -1 Android面试题1. 下列哪些语句关于内存回收的说明是正确的? (b ) A、 程序员必须创建一个线程来释放内存 B、 内存回收程序负责释放无用内_csdn android 移动软件开发 面试题

前端和后端的区别?-程序员宅基地

文章浏览阅读10w+次,点赞105次,收藏254次。有的人认为,前端很好学,后端不好学。也有的人认为,前端不好学,后端好学,归根到底还得看个人兴趣。前端和后端做简单的叙述后端:入门难,深入更难,枯燥乏味,没有太大成就感,看一堆业务逻辑代码。前端:入门简单,先易后难,能看到自己做出来的展示界面,有成就感。前端和后端两者工作的内容和负责的东西是完全的不同01展示的方式不同前端指的是用户..._后端

推荐文章

热门文章

相关标签