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.并查集:并查集直 ...
随机推荐
- iOS UI布局-VFL语言
什么是VFL语言 VFL(Visual Format Language),“可视化格式语言”. VFL是苹果公司为了简化autolayout的编码而推出的抽象语言. 语法说明 H:[cancelBut ...
- 浅析Web API中FromBody属性
比较如下两段代码及测试结果: public class ValuesController : ApiController { // POST api/<controller> public ...
- shell编程:case语句
- react-demo
实现博客动态的评论.动态的点赞.评论的删除. 百度云链接:https://pan.baidu.com/s/199l3iu0qhM6qSe9CBnHFzw 提取码:n4w6
- Ajax技术之使用XMLHttpRequest对象(一)【初始化XMLHttpRequest对象】
在初始化XMLHttpRequest对象时要考虑两种情况,一种是IE浏览器,另一种是非IE浏览器.在IE浏览器中要把XMLHttpRequest实例化一个ActiveX对象 http_request ...
- java outterLoop跳出多重循环用法以及详解
List<CommResultMsg> listresult=new ArrayList<CommResultMsg>(); outterLoop :for (int i = ...
- 初识gispro
因为之前一直用的arcmap,由于项目中用到三维数据的服务发布,需要用到gispro.Gispro与arcmap用法还是有些不同.仅用此文来记录一些简易操作. Gispro简介 ArcGIS Pro是 ...
- Cipher
Description Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Publi ...
- Sitecore CMS中查看标准字段
什么是标准字段? 标准字段是字段及其字段组的集合,用于包含有关Sitecore中项目的可配置元数据.Sitecore中继承自“标准模板”模板的任何项目都将包含这些字段. 可以在“标准模板”模板项找到/ ...
- 类中静态成员变量 && 无法解析的外部符号
[1]如下代码及编译错误 如标题,不做赘述. [2]原因及解决方案 原因:之所以报如上编译错误,因为静态成员变量未初始化. 解决方案:类中静态成员需要在类外进行初始化.其格式为:类型 类名::静态成员 ...