题目传送门

  传送点I

  传送点II

题目大意

  有$n$个商品可以销售。每个商品销售会获得一个利润,但也有一个时间限制。每个商品需要1天的时间销售,一天也只能销售一件商品。问最大获利。

  考虑将出售每个物品尽量外后安排。这样当一个商品不能安排的时候看能不能替换掉它能够出售的时间中盈利最小的商品。

  因此可以将物品排序,这样只用考虑能否让每个物品出售。

  为了找到第一个空闲时间,又因为已经安排的时间不会改变,所以用并查集将已经安排了出售的时间段缩起来。

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 - 并查集 - 贪心的更多相关文章

  1. poj 1456 Supermarket(并查集维护区间)

     题意:有一些货物,每一个货物有价值和卖出的截至日期,每天能够卖一个货物,问能卖出的最大价值是多少. 思路:算法不难想到,按价值降序排列.对于每一件货物,从deadline那天開始考虑.假设哪天空 ...

  2. [POJ2054]Color a Tree (并查集+贪心)

    POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...

  3. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  4. POJ 1456 Supermarket 区间问题并查集||贪心

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 20180309 - C# demo - 1

    using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { ...

  2. ABC2

    OpenCV http://www.cnblogs.com/skyfsm/p/7263773.html http://www.cnblogs.com/skyfsm/p/7613314.html SQL ...

  3. Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关

    Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关 那个上传图片太麻烦了,发布到百度文库了 http://wenku.baidu.com/view/b38ae8 ...

  4. mysql避免脏读

    mysql避免脏读   在MySQL的InnoDB中,预设的Tansaction isolation level 为REPEATABLE READ(可重读) 在SELECT 的读取锁定主要分为两种方式 ...

  5. html5 随机数函数

    function selec(low,high){var ch=high-low+1;return Math.floor(Math.random()*ch+low);}for (var i = 0; ...

  6. XML系列之--创建电文格式的XML(一)

    关于XML,学校那会,老师在口中仅仅提及,自己也未曾深入接触过,仅是些将最基本XML文件内容显示在web定义的表格中之类的简单操作,如今项目中的收发电文涉及到复杂XML的操作.趁此契机好好回顾下XML ...

  7. QString字符串中双引号的梗

    [1]QString字符串不支持双引号 最近做项目(本地环境:WIN10 + QT5.9.2 + VS2017).有个需求,需要实现形如 "key="123456"&qu ...

  8. 什么是UTF-8

    1)开篇啰嗦 感谢这篇博客,在网上转悠了好几天,觉得下面这篇博客我读起来最最容易理解 https://blog.csdn.net/guxiaonuan/article/details/78678043 ...

  9. Linux服务器---流量监控bandwidthd

    Bandwidthd Bandwidthd是一款免费的流量监控软件,它可以用图标的方式展现出网络流量行为,并且可区分出ftp.tcp等各种协议的流量. 1.安装一些依赖软件 [root@localho ...

  10. XWIKI部署安装

    http://www.linuxidc.com/Linux/2016-08/134408.htm