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.


题目大意:

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

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

相比于纯贪心,利用并查集能做出一些优化,就是把每一天都指向这天之前(包括这天)的某一天能卖商品,比如第4、5、6天都有商品了,如果这时候有个商品也是在第六天过期,那么纯贪心,就需要遍历5,4直到3,而并查集就是直接将6指向3。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
#define pii pair<int,int>
pii w[10005];               //存储商品信息
int father[10005];          //并查集用来存该商品最晚能卖的那天
int find(int x){if(father[x] == x)      //直到找到了当天还没有商品需要卖出的那天return x;return father[x] = find(father[x]);
}
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)father[i] = i;for(int i = 1;i <= n;++i){int d = find(w[i].second);if(d){father[d] = d - 1;            //该天卖了过后就不能再卖了,于是指向前一天sum += w[i].first;}}cout<<sum<<"\n";}return 0;
}

其他方法:
纯贪心
贪心 + 优先队列

Supermarket | 贪心 + 并查集相关推荐

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

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

  2. F 魏迟燕的自走棋(思维+贪心+并查集维护联通块/左部点配对边<=2的匈牙利)

    https://ac.nowcoder.com/acm/contest/9984/F 参考:F 魏迟燕的自走棋(贪心+并查集) 将每个人看成一个点,武器的能力值抽象成边,这样就转化成图论的模型了. 然 ...

  3. (POJ - 1456)Supermarket(并查集)

    题目链接:1456 -- Supermarket 原题意比较难理解,下面我给出一个简化版的理解方式:超市里有n个商品.第i个商品必须在保质期(第di天及之前)卖掉,若卖掉可让超市获得pi的利润,但是每 ...

  4. poj1256(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题意:给n件商品的价格和卖出截至时间,每一个单位时间最多只能卖出一件商品,求能获得的最大利润. 思路:首先是贪心,为获得最大利润 ...

  5. HDU多校2 - 6763 Total Eclipse(贪心+并查集)

    题目链接:点击查看 题目大意:给出一张 n 个点和 m 条边组成的无向图,现在每个点都有一个点权,对于每次操作,可以选择一个点以及其周围能够连接的所有点,令其点权减一,当某个点的点权减到 0 时,就相 ...

  6. Kruskal算法:贪心+并查集=最小生成树

    http://www.51nod.com/ Kruskal算法的高效实现需要一种称作并查集的结构.我们在这里不介绍并查集,只介绍Kruskal算法的基本思想和证明,实现留在以后讨论. Kruskal算 ...

  7. 最高的奖励 51Nod - 1163(贪心+并查集)

    有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励.在结束时间之前完成该任务,就可以获得对应的奖励.完成每一个任务所需的时间都是1个单位时间.有时候完成所有任务是不可能的,因为时间上可能会有冲突 ...

  8. Codeforces 437D 贪心+并查集

    这个题目让我想起了上次在湘潭赛的那道跪死了的题.也是最值问题,这个也是,有n个动物园 每个都有权值 然后被m条路径相连接,保证图是连通的,然后求所有的p[i][j]之和.i,j为任意两个zoo,pij ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的"谜之战舰"带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼 ...

最新文章

  1. asp.net 的性能计数器
  2. Direct3D初始化代码含义
  3. python中property方法有用_python中@property和property函数常见使用方法示例
  4. 利用 Cosole 来学习、调试JavaScrip
  5. BZOJ 1079: [SCOI2008]着色方案 记忆化搜索
  6. Codeforces Round #715 (Div. 1) C. Complete the MST 补图 + 思维 + 最小生成树
  7. php 登录记住密码,php 记住密码自动登录
  8. idea springmvc项目搭建_ssm框架整合搭建流程 - 小白小承
  9. MySQL5.5安装版安装教程
  10. 高中计算机考试解析,2019下半年教师资格面试考试高中信息技术试题分析
  11. 计算机如何设置多用户,windows7远程桌面多用户连接怎么设置_win7远程桌面如何设置多人登录-win7之家...
  12. Linux云计算机构-防盗防击防破解三大工具:webshell、ddos-deflate、Medusa
  13. 模拟电子电路基础——放大器理解与梳理
  14. sql查询语句_多字段分类汇总_多表合并
  15. React、Vue等前端项目彻底卸载ServiceWorker,亲测有效
  16. jira 切换 语言_JIRA中的标记语言的语法参考
  17. C语言求素数案例讲解
  18. 使用simulink进行自适应滤波
  19. 计算机已登录 但忘记登录密码怎么办,为你解答电脑开机密码忘记了怎么办
  20. Python金融实战高清中文PDF

热门文章

  1. Keil5 : No Target Connected解决方法
  2. 初学必懂—vue绑定class的几种方式+绑定内联样式总结
  3. 当选改革先锋,马baba的“数字经济”是什么
  4. 什么是TACACS?
  5. TACACS+实验(设备管理)
  6. 腾讯AI全景图首次曝光,解密腾讯的真正基因
  7. OPC UA 和 MQTT
  8. MyBatis多表关联查询
  9. 抖音账号矩阵系统源码.搭建技术开发分享
  10. C 语言 —— 函数定义和函数声明