Supermarket | 贪心


from poj 1456
from acwing 145
时间限制 :2s
内存限制:65M

Description:

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.


题目大意:

一个超市有 一些即将过期商品,且每天只能卖出一件商品,给定你这些过期商品的利润和过期日期,问你这家超市能获得的最多收益是多少。

这个题目一看肯定第一时间就是想到贪心吧!?

对于纯贪心,不用优先队列或者并查集优化的话,那么就是从最高价值的商品开始选,并且每件商品都在过期最后一天卖出,如果该天已经有更高价格的物品需要卖,那么就向前遍历,找到可以卖该商品的那天。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
#define pii pair<int,int>
pii w[10005];               //存储商品信息
bool note[10005];           //用来标记当天是否已经有更高价格得商品需要出售
bool cmp(pii a,pii b){return a.first > b.first;   //贪心,按照利润从大到小排序
}
int n,max_day,sum;            //商品数,最大过期日期,最大利益
int main(){while(cin>>n){max_day = sum = 0;for(int i = 1;i <= n;++i)cin>>w[i].first>>w[i].second,max_day = max(max_day,w[i].second);sort(w + 1,w + 1 + n,cmp);for(int i = 1;i <= max_day;++i)note[i] = false;for(int i = 1;i <= n;++i){if(!note[w[i].second])      //如果该商品的最后一天没有更高利润的商品出售,那么就在当天卖出note[w[i].second] = true,sum += w[i].first;else{                       //否则,往前查找可以用来卖该商品的日子int j = w[i].second;while(note[j])--j;if(j)note[j] = true,sum += w[i].first;}}cout<<sum<<"\n";}return 0;
}

其他方法:
贪心 + 优先队列
贪心 + 并查集

Supermarket | 贪心相关推荐

  1. Supermarket | 贪心 + 并查集

    Supermarket | 贪心 + 并查集 from poj 1456 from acwing 145 时间限制 :2s 内存限制:65M Description: A supermarket ha ...

  2. nyoj 208 Supermarket(贪心)

    Supermarket 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 A supermarket has a set Prod of products on sale. ...

  3. poj 1456 Supermarket (贪心, 并查集)

    链接: http://poj.org/problem?id=1456 题目: Description A supermarket has a set Prod of products on sale. ...

  4. poj 1456 Supermarket 贪心+并查集(个人感觉有点难判断出来

    poj 1456 这第一眼还觉得只要贪心就可以了,但是emmm看了大佬的题解居然真的要用到并查集= = 大佬清晰的思路 大佬舒服的代码 #pragma warning(disable:4996) #i ...

  5. UVa1316 Supermarket(贪心)

    问题:超市里有 n个商品出售,每个商品如果在d截止时间内出售,可以获取利润p.每个商品出售需要一个时间单位.问最大利润是多少? 思路: 第一种方法是根据截止时间升序排列,同时依赖优先级队列,当商品的截 ...

  6. 训练指南第一部分解题报告

    主要是提供训练指南第一部分解题报告链接,后面会持续更新中 307 - Sticks  (DFS+剪枝) 11292 - Dragon of Loowater (贪心) 11729 - Commando ...

  7. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  8. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

最新文章

  1. uniny 物体运动到一个点停止_隐藏的几何:各类随机物体中的深层联系
  2. python有时候没有智能提示_python没有报错提示
  3. 在linux系统使用nginx部署静态网页
  4. pytorch 优化GPU显存占用,避免out of memory
  5. halcon知识:对空图像的系列操作
  6. cache目录没有权限
  7. UI设计素材模板|wireframe线框图设计要点
  8. python3--迭代器
  9. Python 进阶——重访 list
  10. Insert Delete GetRandom O(1)
  11. 笔记本电脑自带键盘禁用与恢复
  12. 微信小程序 微信小程序框架API
  13. 民营股份制企业是什么意思
  14. 故障效果,制作抖音效果的幻影海报
  15. 将电脑的无线网通过有线分享给其他设备
  16. 运营_APP的常见盈利模式
  17. 程序员在上海税前12000的工资,真实发到手能拿到多少?
  18. 左侧颜面部起疱,疼痛剧烈2天-牙博士
  19. Java web speach api_HTML5 Web Speech API,让网站更有趣
  20. java数据类型(java数据类型有哪些)

热门文章

  1. dubbo zookeeper not connected
  2. excel套用表格样式转为区域
  3. 【闪电侠学netty】第6章 客户端与服务端双向通信
  4. 1. 使用Popup组件自定义弹框提示页面
  5. uniapp的plus获取数据
  6. MySQL 报错处理汇总(持续更新)
  7. 【逻辑与计算理论】λ演算与组合子逻辑概念简介
  8. visdom启动失败_Windows上安装并启动visdom
  9. 从斗鱼顶级女主播来思考直播间画面的布局
  10. Java 在PDF中添加工具提示|ToolTip