【POJ - 1456】Supermarket (贪心,优先队列 或并查集)

题干:

A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σ x∈Sellpx. An optimal selling schedule is a schedule with a maximum profit. 
For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80. 


Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products. 

Input

A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.

Output

For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.

Sample Input

4  50 2  10 1   20 2   30 17  20 1   2 1   10 3  100 2   8 25 20  50 10

Sample Output

80
185

Hint

The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

题目大意:

超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润. 
每天只能卖一个商品.
现在你要让超市获得最大的利润。

解题报告:

   优先队列就行了,考虑成每一个cur天的决策,如果这一天还不到截止日期,那就直接push,并且把cur++。如果到了截止日期,那就只能看看需不需要更新pq中的元素,最后元素的个数一定就是答案。这题还有并查集做法:【51Nod - 1163】最高的奖励,按权值排序,遍历来找到可以最早安放的日期。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<bitset>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> pii;
const int MAX = 2e5 + 5;
int n;
pii p[MAX];
int main()
{while(~scanf("%d",&n)) {for(int i = 1; i<=n; i++) cin>>p[i].second>>p[i].first;sort(p+1,p+n+1);priority_queue<int,vector<int>,greater<int> > pq;int cur = 1;for(int i = 1; i<=n; i++) {if(cur <= p[i].first) {pq.push(p[i].second);cur++;}else {if(p[i].second > pq.top()) pq.pop(),pq.push(p[i].second);}} int ans = 0;while(!pq.empty()) ans += pq.top(),pq.pop();printf("%d\n",ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440680.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 558C】Amr and Chemistry(位运算,bfs,计数,思维,tricks)

题干&#xff1a; Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n different types of chemicals. Each chemical i has an initial volume of ailiters. For this experiment, Amr has to mix all th…

我家云刷android系统教程,我家云刷机教程——小白详细版(篇二)

#大男孩的快乐#征稿活动火热进行中。只要投稿就有50金币等你拿&#xff0c;更有三千元乐高大奖与达人Z计划专属权益等待优秀的你~>活动详情戳这里<前两天发了一篇我家云的刷机教程&#xff0c;没想到大家这么有兴趣&#xff0c;讨论的异常激烈。看了大家的评论才发现之前…

【Loj - 515】贪心只能过样例(暴力,或01背包 + bitset)

题干&#xff1a; 题目描述 输入格式 第一行一个数 n。 然后 n 行&#xff0c;每行两个数表示 ai​,bi​。 输出格式 输出一行一个数表示答案。 样例 样例输入 5 1 2 2 3 3 4 4 5 5 6 样例输出 26 数据范围与提示 解题报告&#xff1a; 注意到要求统计种类数&#xf…

html loader的作用,webpack认识loader的作用

举例&#xff1a;如果希望在.html文件中使用style.css样式&#xff0c;我们以前只学习过一种方式&#xff1a;直接在.html中通过link的方式来引入 &#xff0c;这是传统的做法&#xff0c;在webpack语境下&#xff0c;我们将选择一条不同的道路&#xff1a;在js文件中引入了css…

5可视化数据大屏模板_可视化大屏模板分享

3个月前的一天&#xff0c;老板找到我&#xff1a;“小王&#xff0c;数据怎么才能产生让人惊艳的感觉呢&#xff1f;”我说&#xff1a;“肯定是用代码让程序员操作一下&#xff0c;再让设计师做一下配色&#xff0c;最好还能是数据实时变化的那种&#xff0c;简直就和电影里一…

c#12星座速配代码_白羊座今日运势|2020/12/11

整体运势&#xff1a;★★★☆☆爱情运势&#xff1a;★★☆☆☆事业运势&#xff1a;★★☆☆☆财富运势&#xff1a;★★★☆☆幸运数字&#xff1a;7速配星座&#xff1a;金牛座幸运颜色&#xff1a;橙色幸运时刻&#xff1a;12:00-14:00整体运势&#xff1a;接收的消息会比…

怎么去掉拼写检查的红线_发动机温度过高,水箱“开锅”,我们到底该怎么办?...

车子开久了&#xff0c;难免会出现一些小故障&#xff0c;今天我们就来说一下关于发动机水温过高的问题&#xff0c;这也是老司机常说的“开锅”。发动机水温正常情况是在80-90摄氏度之间&#xff0c;一旦超过这个温度&#xff0c;也就是水温表过了红线的时候&#xff0c;我们就…

【ZOJ - 2972】Hurdles of 110m (dp)

题干&#xff1a; In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperity of China and Beijing Olympics is to be a festival for people all over the world as well. Liu Xiang is one of the famous Olympic athletes i…

格式说明_ISO11784/85 FDX-B动物芯片格式写码说明

植入式生物芯片口蹄疫、疯牛病、禽流感、非洲猪瘟的蔓延严重危及人类生命安全&#xff0c;引发了人们对动物性食品生产全过程安全追溯管理的重视。因此&#xff0c;有效地利用非接触式无线射频技术(RFID)对其进行控制成为有效的重要手段。一、国际动物识别标准介绍ISO 11784&am…

1至9填入九个方框内_防水涂料和施工堵漏的“五大技巧”和“九个细节”

防水涂料和施工堵漏的“五大技巧”和“九个细节”很多业主会在留言里问我家里水管堵了、房顶渗水怎么办&#xff0c;所以防水问题不可忽视。让您住的安心&#xff0c;您楼下的邻居也住的安心。对于防水问题&#xff0c;一定要在最初进行装修的时候就特别注意。下面这些是专业人…

限制 计算机中 某用户上网 win7,利用win7限制上网时间的方法

随着电脑的普及&#xff0c;很多家长为了让自己的孩子能有更广泛的知识&#xff0c;都给自己的孩子买了电脑&#xff0c;可现在很多孩子使用电脑却很少耗费在学习上&#xff0c;更多的时间是在娱乐&#xff0c;这样不仅影响学习而且还影响着视力&#xff0c;这样的问题相信困扰…

【ZOJ - 2976】Light Bulbs (枚举,暴力)

题干&#xff1a; Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get married this year. He bought a new house for his family and hired a company to decorate his house. Wildleopard and his…

为什么计算机编程以英语为主,为什么做编程都用英文,中文不行吗?听程序员解释完,总算明白了...

今天我们科普的话题是&#xff1a;为什么做编程都用英文&#xff0c;中文不行吗&#xff1f;听程序员解释完&#xff0c;总算明白了不知道大家最近有没有追过一部电视剧&#xff0c;它是《亲爱的热爱的》&#xff0c;由李现和杨紫主演的。主要讲述了这对情侣发生的浪漫爱情故事…

html的技术标准网站,HTML5标准最新技术预览

已经存在近十年的HTML4已经成为不断发展的Web开发领域的瓶颈&#xff1b;HTML5标准在此时显得尤为重要。每天都有新颖而创新的网站出现&#xff0c;全方位地拓展HTML的边界。Web开发者正在寻求新的技术&#xff0c;提供更强大的功能&#xff0c;那些因为脚本语言和浏览器的限制…

计算机网络技术实验,计算机网络技术实验

计算机网络技术实验 实 验 一 (2课时) 课题(项目)名称: 基本网络命令 计划学时: 2学时 实验目的 1. 了解网络命令的基本使用方法 2. 掌握常用网络命令的应用 3. 掌握网络通达性的基本测试方法 实验要求 1. 全面了解各种网络命令 2. 对常用的网络命令&#xff0c;上机测试运行 3…

html div分钟刷新一次啊代码_接口测试平台代码实现57首页重构5

本节我们接着开发首页&#xff0c;主要内容是 让左侧的请求记录和右侧的请求模块 联动起来。具体联动需求&#xff1a;右侧请求一次之后&#xff0c;左侧生成记录点击左侧记录&#xff0c;右侧则会显示这次记录的请求数据我们本节课的内容是想办法实现1-生成记录。我们知道&…

会计专业领域计算机应用系统,信息技术在会计专业领域中的应用

在当今这个信息时代,信息技术对会计专业产生了重大的变革。本文在详细分析会计信息化特征的基础上,阐述了由于信息技术应用对会计职能产生的影响,并提出了如何更好的在会计专业领域推广信息技术。信息技术在会计专业领域中的应用郑晨(福建农林大学计算机与信息学院&#xff0c;…

固定 顶部_纹络型温室大棚顶部通风样式及效率对比

纹络型温室大棚是一种使用玻璃作为覆盖材料的温室大棚&#xff0c;该种温室大棚由于抗风雪能力强&#xff0c;环境控制能力强、使用年限长&#xff0c;目前在我国的南北地区大量建造。由于该型号的玻璃温室大棚单体占地面积能够达到几十亩地&#xff0c;因此单靠墙面通风无法满…

微型计算机智能体重评测,荣耀智能体脂秤WiFi版评测:17项身体指标 比你更懂你...

原标题&#xff1a;荣耀智能体脂秤WiFi版评测&#xff1a;17项身体指标 比你更懂你[PConline 评测]俗话说冬天不减肥&#xff0c;夏天徒伤悲&#xff0c;每个人都希望拥有穿衣显瘦、脱 衣有肉的完美身材。但往往理想很丰满&#xff0c;现实很骨感&#xff0c;很多人都陷入减肥的…

人脑意识转入量子计算机,人脑产生意识:可能是因为量子纠缠

图片来源&#xff1a;quanta magazine一提到“量子意识”这个词语&#xff0c;大多数物理学家都会选择避而不谈&#xff0c;因为这个词语听起来好像有点“民科”&#xff0c;甚至让人联想宗教或者玄学。不过出人意料的是&#xff0c;量子效应可能真的在人类的认知过程中起到了一…