poj 1456 Supermarket - 并查集 - 贪心
考虑将出售每个物品尽量外后安排。这样当一个商品不能安排的时候看能不能替换掉它能够出售的时间中盈利最小的商品。
因此可以将物品排序,这样只用考虑能否让每个物品出售。
为了找到第一个空闲时间,又因为已经安排的时间不会改变,所以用并查集将已经安排了出售的时间段缩起来。
Code
/**
* poj
* Problem#1456
* Accepted
* Time: 47ms
* Memory: 768k
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
typedef bool boolean; typedef class Product {
public:
int profit;
int deadline; Product() { } boolean operator < (Product b) const {
return profit > b.profit;
}
}Product; const int N = 1e4 + ; int n, res;
int pre[N];
Product *ps;
boolean vis[N]; inline boolean init() {
if (scanf("%d", &n) == EOF)
return false;
res = ;
ps = new Product[(n + )];
for (int i = ; i <= n; i++)
scanf("%d%d", &ps[i].profit, &ps[i].deadline);
return true;
} int findPre(int p) {
if (!p) return ;
if (!vis[p]) {
vis[p] = true;
return p;
}
return pre[p] = findPre(pre[p]);
} inline void solve() {
sort(ps + , ps + n + );
memset(vis, false, sizeof(vis));
for (int i = ; i <= ; i++)
pre[i] = i - ;
for (int i = ; i <= n; i++) {
int p = findPre(ps[i].deadline);
if (p)
res += ps[i].profit;
}
printf("%d\n", res);
delete[] ps;
} int main() {
while (init())
solve();
return ;
}
poj 1456 Supermarket - 并查集 - 贪心的更多相关文章
- poj 1456 Supermarket(并查集维护区间)
题意:有一些货物,每一个货物有价值和卖出的截至日期,每天能够卖一个货物,问能卖出的最大价值是多少. 思路:算法不难想到,按价值降序排列.对于每一件货物,从deadline那天開始考虑.假设哪天空 ...
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 1456——Supermarket——————【贪心+并查集优化】
Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 1456 Supermarket(贪心+并查集)
题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...
- POJ 1456 Supermarket(贪心+并查集优化)
一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2 10 1 20 2 10 1 50+20 50 2 40 ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
随机推荐
- 一个vue的循环列表,里面的按钮的移入事件
需求:移入的时候,互相关注变成取消关注 移入移出事件传参$event,把这个参数打印出来看就可以搞定,而不是移入的时候,文本都改变,只改变当前行 的文本 <p @mouseover=" ...
- Ecshop表结构 order_info
CREATE TABLE IF NOT EXISTS `ecs_order_info` ( `order_id` mediumint(8) unsigned NOT NULL AUTO_INCREM ...
- HDU 1568 Fibonacci(大数前4位)
转载自:http://blog.csdn.net/thearcticocean/article/details/47615241 分析:x=1234567.求其前四位数: log10(x)=log10 ...
- Ajax三级联动
全国省市县查询 html代码 <!doctype html> <html> <head> <meta charset="utf-8"> ...
- notepad怎么把空格替换成回车?
替换时选中“使用正则表达式”查找里输入\s替换里输入\r然后“全部替换”即可
- django 网站的搭建(1)
使用 python django 模块来搭建自己的博客网站. 本人环境:阿里云centos7+django1.10+python3.5 使用工具:putty + winscp 1.首先安装python ...
- jsp无法访问
一直无法访问jsp: 由于Spring boot使用的内嵌的tomcat,而内嵌的tamcat是不支持jsp页面的,所有需要导入额外的包才能解决. <!-- 解决jsp无法访问 --> & ...
- C# 自定义承载控件
例如ToolStripTextBox类,里面的封装就是一个TextBox控件..NET的类库提供了ToolStripComBox和ToolStripProgressBar两个类.这些类都是从ToolS ...
- Linux基础命令---apwatch
arpwatch arpwatch指令可以监听网络设备和ip地址的对应关系,将发现的信息发送到系统日志“/var/log/message”. 此命令的适用范围:RedHat.RHEL.Ubu ...
- springboot 接收post和get请求
接收post请求: @RequestMapping(value = "/api/v1/create_info", method = RequestMethod.POST) publ ...