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.并查集:并查集直 ...
随机推荐
- 巧用CurrentThread.Name来统一标识日志记录(java-logback篇)
▄︻┻┳═一Agenda: ▄︻┻┳═一巧用CurrentThread.Name来统一标识日志记录 ▄︻┻┳═一巧用CurrentThread.Name来统一标识日志记录(续) ▄︻┻┳═一巧用Cur ...
- opencv-resize()放缩函数简介
主要介绍函数resize(); 图像缩放的效果图如下: 主程序代码及函数解释如下所示: /******************************************************* ...
- linux 小笔记
//强杀pid2306的进程 kill -9 2306 //查看80端口占用情况 lsof -i :80 //杀死mysql的所有进程 sudo killall mysqld //关闭占用80端口的所 ...
- centos7上安装nodejs
1. 确保安装编译工具 $ yum -y install gcc gcc-c++ 2. 获取官网源码 : $ wget https://nodejs.org/dist/v9.9.0/node-v9.9 ...
- TP增删改
增加数据 <?php namespace Home\Controller; use Think\Controller; class IndexController extends Control ...
- php实现多进程
转:http://www.jb51.net/article/71238.htm cd php-version/ext/pcntl phpize ./configure && make ...
- html5随机背景颜色
<script>var colors=['red','blue','green','orange','yellow']; function bcolor(){ var ...
- SQL性能优化前期准备-清除缓存、开启IO统计
文章来至:https://www.cnblogs.com/Ren_Lei/p/5669662.html 如果需要进行SQl Server下的SQL性能优化,需要准备以下内容: 一.SQL查询分析器设置 ...
- CSS选择符-----关系选择符
包含选择符(E F) 选择所有被E元素包含的F元素 与 子选择符(E>F) 不同的是,包含选择符将会命中所有符合条件的后代,包括儿子,孙子,孙子的孙子... <! ...
- javascript_函数式_链式编程