poj1456 Supermarket 贪心+并查集
题目链接: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 贪心+并查集的更多相关文章
- POJ 1456 Supermarket(贪心+并查集优化)
一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2 10 1 20 2 10 1 50+20 50 2 40 ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
- POJ 1456 Supermarket(贪心+并查集)
题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- POJ 1456——Supermarket——————【贪心+并查集优化】
Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj1456(贪心+并查集)
题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...
- Supermarket(贪心/并查集)
题目链接 原创的博客 题意: 超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润. 每天只能卖一个商品. 现在你要让超市获得最大的利润. n , p[i], ...
- POJ 1456 (贪心+并查集) Supermarket
有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...
- POJ1456 Supermarket —— 贪心 + 路径压缩优化
题目链接:http://poj.org/problem?id=1456 Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
随机推荐
- 内部yum仓库制作
有些安装收到网络隔离(申请一个到DMZ区的通行证很困难) 使用yum的命令工具,在有网络环境下同步我们的yum仓库,并用http服务器代理和制作repo源进行内部安装. 实操: [root@maste ...
- 【pytorch】pytorch-backward()的理解
pytorch-backword函数的理解 函数:\(tensor.backward(params)\) 这个params的维度一定要和tensor的一致,因为tensor如果是一个向量y = [y1 ...
- Ubuntu 16.04 安装opencv3.4.5/cuda/caffe并使用jni笔记
因操作失误,误卸开发机NVIDIA显卡驱动,先更新操作日志如下: 1>NVIDIA驱动重装 1.卸载系统里的Nvidia残余 sudo apt-get purge nvidia* 2.把显卡驱动 ...
- 介绍一款自动给添加不同浏览器CSS3前缀的插件~Autoprefixer(附其他前端开发插件)
正文 自动给CSS文件添加不同浏览器的CSS3前缀:Autoprefixer 安装 只需兼容主流浏览器 正常情况使用:(在书写完的CSS样式文件中,按F1,选择Autoprefixer CSS) 这时 ...
- springMVC统一异常处理
Spring MVC处理异常有3种方式: 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver: 实现Spring的异常处理接口HandlerExc ...
- 第八周java学习总结
学号 20175206 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章:泛型与集合框架 主要内容 泛型 链表 堆栈 散列映射 树集 树映射 重点和难点 重点:泛型和集合的使 ...
- Springboot集成Spring Batch
Spring官网 (https://spring.io/projects/spring-batch#overview)对Spring Batch的解释: 一个轻量级的.全面的批处理框架,用于开发对企 ...
- 五十四、linux 编程——TCP 编程模型
54.1 编程模型介绍 54.1.1 TCP 客户端服务器编程模型 客户端调用序列 调用 socket 函数创建套接字 调用 connect 连接服务器端 调用 I/O 函数(read/write) ...
- Python——Selenium & Chrome Driver配置
1.CMD下载安装selenium pip install selenium 2.python运行: from selenium import webdriver browser = webdrive ...
- 20155324《网络对抗》Exp1 PC平台逆向破解(5)M
20155324<网络对抗>Exp1 PC平台逆向破解(5)M 实验目标 本次实践的对象是一个名为~pwn1~的~linux~可执行文件. 该程序正常执行流程是:~main~调用~foo~ ...