题目链接:http://poj.org/problem?id=1456

题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi <= 10000, 1 <= di <= 10000),物品必须在保质期之前卖出。且每天只能卖出一个物品,问如何安排才能使卖出的总价格最大。

这道题贪心的思想很明显,先将物品的价格按照从大到小排序,再按照该顺序卖物品,如果存在不超过保质期的最大可用日期,则该物品能够卖出,并将这一天标记。关键在于如何找这个日期,就是在一个有序序列中查找小于等于当前日期的最大值,再将其删除,继续查找。这个可以用set来做。

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stack>
#include <set>
#include <istream>
#include<queue>
#include<stack>
#include <cstring>
#include <string>
//#include <climits>
using namespace std;
struct products {
int money, day;
};
bool cmp(products a, products b) {
if (a.money != b.money)
return a.money > b.money;
return a.day>b.day;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
set<int> s;
products *p = new products[n];
int md = 0;
for (int i = 0;i < n;i++) {
scanf("%d%d", &p[i].money, &p[i].day);
md = max(p[i].day, md);
}
for (int i = 1;i <= md;i++)
s.insert(i);
sort(p, p + n, cmp);
int ans = 0;
for (int i = 0;i < n && !s.empty();i++) {
if (*s.begin() > p[i].day)
continue;
set<int>::iterator iter = s.upper_bound(p[i].day);
iter--;
//cout << *iter << -1 << endl;
if (*iter <= p[i].day) {
ans += p[i].money;
s.erase(iter);
}
}
printf("%d\n", ans);
delete[]p;
}
}

  

除了这种解法外,我们还可以用并查集来维护,初始化每个日期i的父亲为自己,当当前的日期i被占用后,将其父亲设为前一天的日期i-1,每次查找i的最大可用日期即查找i的祖先即可,由于使用路径压缩,所以查找的复杂度很低,比用set块

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
class union_find_set {
public:
union_find_set(int n) {
fa = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++)
fa[i] = i;
}
~union_find_set()
{
delete fa,rank;
};
int find(int x) {
if (fa[x] == x)
return x;
return fa[x] = find(fa[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
fa[x] = y;
else {
fa[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool same(int x, int y) {
if (find(x) == find(y))
return 1;
return 0;
}
int n;
int *fa,*rank;
}; struct products {
int money, day;
}; bool cmp(products a, products b) {
return a.money > b.money;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
products *p = new products[n];
int md = 0;
for (int i = 0;i < n;i++) {
scanf("%d%d", &p[i].money,&p[i].day);
md = max(p[i].day, md);
}
union_find_set P(md + 1);
sort(p, p + n, cmp);
int ans = 0;
for (int i = 0;i < n;i++) {
int t = P.find(p[i].day);
if (t > 0) {
ans += p[i].money;
P.fa[t] = t - 1;
}
}
printf("%d\n", ans) ;
delete p;
}
}

  

poj1456 Supermarket 贪心+并查集的更多相关文章

  1. POJ 1456 Supermarket(贪心+并查集优化)

    一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2  10 1   20 2   10 1    50+20 50 2  40 ...

  2. POJ_1456 Supermarket 【并查集/贪心】

    一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...

  3. POJ 1456 Supermarket(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...

  4. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

  5. POJ 1456——Supermarket——————【贪心+并查集优化】

    Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. poj1456(贪心+并查集)

    题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...

  7. Supermarket(贪心/并查集)

    题目链接 原创的博客 题意: 超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润. 每天只能卖一个商品. 现在你要让超市获得最大的利润. n , p[i], ...

  8. POJ 1456 (贪心+并查集) Supermarket

    有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...

  9. POJ1456 Supermarket —— 贪心 + 路径压缩优化

    题目链接:http://poj.org/problem?id=1456 Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. WebSocket介绍,与Socket的区别

    WebSocket介绍与原理 WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex).一开始的握手需要借助HTTP请求完成. ——百 ...

  2. codeforces463D

    Gargari and Permutations CodeForces - 463D Gargari got bored to play with the bishops and now, after ...

  3. html-webpack-plugin不输出script标签的方法

    那就是修改源码 约550行: if (!this.options.disableScript) { if (this.options.inject === 'head') { head = head. ...

  4. [debian]use ISO as debian apt source / 使用ISO文件作为apt源

    准备文件: debian-9.8.0-amd64-DVD-1.iso debian-9.8.0-amd64-DVD-2.iso debian-9.8.0-amd64-DVD-3.iso 挂载: roo ...

  5. php 两个数组,若键相同,则值合并

    <?php $arr1 = array('9' => '4.08', '10' => '0.10', '11' => '4.08', '12' => '0.01'); $ ...

  6. antd Tree组件中,自定义右键菜单

    最近项目中,有一个需求是自定义antd的Tree组件的右键菜单功能. 直接上代码 class Demo extends Component { state = { rightClickNodeTree ...

  7. eclipse 包 取消代码第一行package包名 自动补全时取消自动引入包名 修改名字 取消引用 自动导入publilc static void main(String[] args) {}

    --项目 --包 包是为了管理类文件,同个包下不允许同名类文件,但不同包就可以,把类放在包里是规范 (https://zhidao.baidu.com/question/239471930532952 ...

  8. kubernetes之管理容器的计算资源

    资源类型 CPU 和 memory 都是 资源类型.资源类型具有基本单位.CPU 的单位是 core,memory 的单位是 byte.这些都统称为计算资源. CPU含义: CPU 资源的限制和请求以 ...

  9. IDEA+Maven配置MyBatis的初体验(坑点总结)

    起因 在老师的推荐下,我跟着这篇文章https://www.jb51.net/article/70923.htm尝试MyBatis的初次调试运行.途中困难重重,由于教程中的开发环境是eclipse,项 ...

  10. sql0001

    001.   https://blog.csdn.net/qinshi965273101/article/details/81907658 002.    https://blog.csdn.net/ ...