【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…

axure 转换为html,AxureRP教程AxureRP如何生成HTML文件

1.正常打开一份已经设计好的RP&#xff0c;如下截图所示&#xff1a;2.点击上方菜单的“发布”按钮&#xff0c;在弹出的选项中单击“生成HTML文件”&#xff0c;详细操作如下图标红位置。3.在弹出的新窗口中&#xff0c;可以设定HTML文件保存的文件夹位置&#xff0c;这个位置…

【牛客 - 373B】666RPG(线性计数dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/373/B 来源&#xff1a;牛客网 在欧美&#xff0c;“666”是个令人极其厌恶和忌讳的数&#xff0c;被称为“野兽数”。 相传&#xff0c;尼禄&#xff0c;这位历史上以暴君著称的古罗马皇帝&#xff0…

我家云刷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…

html5文件域的自动获取,HTML5 文件域+FileReader 读取文件(一)

在HTML5以前&#xff0c;HTML的文件上传域的功能具有很大的局限性&#xff0c;这种局限性主要体现在如下两点&#xff1a;每次只能选择一个文件进行上传客户端代码只能获取被上传文件的文件路径&#xff0c;无法访问实际的文件内容一、FileList对象和File对象HTML5为typefile 的…

计算几何 模板

计算几何模板&#xff1a; #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #d…

html中如何让三个方块并排,html – 并排设置两个div,然后设置第三个div

我如何并排设置两个div,而下面的第三个div设置为这样.我当前的代码如下所示,将div放在name div之后Name6:30 PMNoteCSS&#xff1a;#contact_table_data {width:inherit;height:inherit;background-color:#99cc33;max-width:400px;}#info_div_name {width:auto;height:auto;pad…

【ZOJ - 4019】Schrödinger's Knapsack (dp,背包,贪心,组内贪心组间dp)

题干&#xff1a; 有两种物品&#xff0c;k分别为k1&#xff0c;k2&#xff0c;有大小各不一的这两种物品若干&#xff0c;放入容量为c的背包中&#xff0c;能获得求最大的值。放的顺序会影响结果。每次放入一物品&#xff0c;其获得的值都可以用vkr计算&#xff0c;r表示放入…

html 闪烁字,HTML最简单的文字闪烁代码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼Titlekeyframes blink{0%{opacity: 1;}50%{opacity: 1;}50.01%{opacity: 0;}100%{opacity: 0;}}-webkit-keyframes blink {0% { opacity: 1; }50% { opacity: 1; }50.01% { opacity: 0; }100% { opacity: 0; }}-moz-keyframes blin…

【ZOJ - 4020 】Traffic Light (bfs,分层图)

题干&#xff1a; n*m矩阵a.若a[i][j]1则可以往左右走,若a[i][j]0 则可以往上下走. 每一秒可以按上述规则移动,并且每秒钟矩阵所有的值翻转。 n*m<1e5.问从(sx,sy)到(tx,ty)的最短时间. 解题报告&#xff1a; 这题因为不带权值所以不需要考虑Dijkstra&#xff0c;可以根据…

在计算机应用中mis,在计算机的应用中,“MIS”表示

阅读下列材料&#xff0c;回答有关问题&#xff1a;地理信息系统(GIS)&#xff0c;也称作地理资讯系统&#xff0c;是一门综合性学科&#xff0c;已经广泛地应用在不同的领域&#xff0c;是用于输入、存储、查询、分析和显示地理数据的计算机系统。GIS属于信息系统的一类&#…

【牛客 - 369C】小A与欧拉路(bfs树的直径)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/369/C 来源&#xff1a;牛客网 小A给你了一棵树&#xff0c;对于这棵树上的每一条边&#xff0c;你都可以将它复制任意&#xff08;可以为0&#xff09;次&#xff08;即在这条边连接的两个点之间再…

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

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

【牛客 - 188C】水图(bfs树的直径,思维)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/188/C 来源&#xff1a;牛客网 小w不会离散数学&#xff0c;所以她van的图论游戏是送分的 小w有一张n个点n-1条边的无向联通图,每个点编号为1~n,每条边都有一个长度 小w现在在点x上 她想知道从点x出…

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

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

delphi webbrowser 显示 html,delphi webbrowser

delphi 怎么判断webbrowser打开网页成功?unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, SHDocVw; type TForm1 class(TForm) WebBrowser1: TWebBrowser; Button1: TButton; Edit1: …

【蓝桥杯官网训练 - 历届试题】对局匹配(dp,思维,取模)

题干&#xff1a; 问题描述 小明喜欢在一个围棋网站上找别人在线对弈。这个网站上所有注册用户都有一个积分&#xff0c;代表他的围棋水平。   小明发现网站的自动对局系统在匹配对手时&#xff0c;只会将积分差恰好是K的两名用户匹配在一起。如果两人分差小于或大于K&#…

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

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

计算机网络基础端口号,1 计算机网络基础练习

Ch1 计算机网络基础1.以下网络结构当中哪一种的安全性和保密性较差A. mesh(网状)B. treeC. busD. star2.OSI 参考模型中的OSI 表示的是A. Organization Standard InstituteB. Organization Standard InterconnectionC. Open System InternetD. Open System Interconnection3.多…