题目链接: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 贪心+并查集的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. poj1456(贪心+并查集)

    题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...

  7. Supermarket(贪心/并查集)

    题目链接 原创的博客 题意: 超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润. 每天只能卖一个商品. 现在你要让超市获得最大的利润. n , p[i], ...

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

    有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...

  9. POJ1456 Supermarket —— 贪心 + 路径压缩优化

    题目链接:http://poj.org/problem?id=1456 Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. Ubuntu 16.04 RabbitMq 安装与运行(安装篇)

    Ubuntu 16.04 RabbitMq 安装与运行(安装篇) 2018年08月15日 15:05:24 我跟吴彦祖四六开 阅读数:1966   前言 目前公司用阿里云 + redis 的方式实现的 ...

  2. [2019.03.25]Linux中的查找

    TMUX天下第一 全世界所有用CLI Linux的人都应该用TMUX,我爱它! ======================== 以下是正文 ======================== Linu ...

  3. SPOJ DIVCNT2

    SPOJ DIVCNT2 题目大意: 求\(S2(n)=\sum_{i=1}^{n}\sigma_0{(i^2)}\) . 题解 我们可以先考虑括号里只有一个\(i\)的情况,这样,我们把\(i\)分 ...

  4. Apache POI 示例

    Apache POI 3.17 Javadocs 用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office(Excel.WO ...

  5. elasticsearch 介绍

    一.什么是elasticsearch Elasticsearch是一个基于Lucene的高度可伸缩的分布式的开源全文搜索和分析引擎.它允许您快速.实时地存储.搜索和分析大量数据.它通常用作底层引擎/技 ...

  6. .NET框架 - NETCORE + API + EF + MYSQL

    .NET框架 - NETCORE + API + EFCORE + MYSQL 1. 新建项目: 本文中使用 框架 .netcore2.2 . 2. 生成项目框架 3 安装MYSQL插件 点击“工具” ...

  7. mysql中整数类型后面的数字,比如int(11),11代表11个字节吗?

    原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...

  8. Shell脚本统计文件行数

    Shell脚本统计文件行数 转自 http://www.jb51.net/article/61943.htm    示例:row_count.sh文件 awk '{print NR}' row_cou ...

  9. EF Core系列

    一. 二. 三. 系列章节 第一节:EF Core简介和CodeFirst和DBFirst两种映射模式(以SQLite和SQLServer为例) 第X节:XXXXXXXXXXXXXXXXXXXXXXX ...

  10. Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

    先上效果图: 假设 tab bar items 有5个.tag为0,1,2,3,4.storyboard中tab bar controller继承的class叫做xxxVC. class xxxVC: ...