POJ - 1456 贪心+并查集
做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可。时间复杂度O(n * d)当d都为10000时,很容易超时。由于这题数据比较水,所有贪心未超时。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 10000 + 5;
struct node{
int prof, dead;
bool operator < (const node &p) const {
return prof > p.prof;
}
}a[maxn];
int vis[maxn];
int main() {
int n;
while(scanf("%d", &n) == 1) {
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].prof, &a[i].dead);
}
sort(a, a+n);
memset(vis, 0, sizeof(vis));
int ans = 0;
for(int i = 0; i < n; ++i) {
int flag = 0;
for(int j = a[i].dead; j > 0; --j) {
if(!vis[j]) {
flag = vis[j] = 1;
break;
}
}
if(flag) ans += a[i].prof;
}
printf("%d\n", ans);
}
return 0;
}
做法二:先按照利润排序,然后并查集--当某个时间点被占据,它的下一个可用时间点为t - 1,将二者合并即可。复杂度n*logn
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 10000 + 5;
struct node{
int prof, dead;
bool operator < (const node &p) const {
return prof > p.prof;
}
}a[maxn];
int p[maxn];
int find(int x) {
return x == p[x] ? x :p[x] = find(p[x]);
}
void unionset(int x, int y) {
int rx = find(x), ry = find(y);
if(rx != ry) p[rx] = ry;
}
int main() {
int n;
while(scanf("%d", &n) == 1) {
for(int i = 0; i < maxn; ++i) p[i] = i;
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].prof, &a[i].dead);
}
sort(a, a+n);
int ans = 0;
for(int i = 0; i < n; ++i) {
int u = find(a[i].dead);
if(u > 0) ans += a[i].prof;
unionset(a[i].dead, u-1);
}
printf("%d\n", ans);
}
return 0;
}
做法三:利用优先队列,按照时间从大到小依次放入商品,保证当前的商品的截止期限都大于等于当前时间即可。可以理解为当有多个商品需要在同一天完成优先选择价值最大的。
复杂度O(n * lgn)
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;
struct node{
int prof, dead;
}a[maxn];
bool cmp(const node &a, const node &b) {
return a.dead > b.dead;
}
bool operator < (const node &p1, const node &p2){
return p1.prof < p2.prof;
}
int main() {
int n;
while(scanf("%d", &n) == 1) {
int maxt = 0;
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].prof, &a[i].dead);
maxt = max(maxt, a[i].dead);
}
sort(a, a+n, cmp);
priority_queue<node>q;
int ind = 0, ans = 0;
for(int i = maxt; i > 0; --i) {
while(ind < n && a[ind].dead == i) {
q.push(a[ind++]);
}
if(!q.empty()) {
ans += q.top().prof;
q.pop();
}
}
printf("%d\n", ans);
}
return 0;
}
如有不当之处欢迎指出!
POJ - 1456 贪心+并查集的更多相关文章
- POJ 1456 (贪心+并查集) Supermarket
有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...
- Supermarket POJ - 1456 贪心+并查集
#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...
- poj 1456 Supermarket - 并查集 - 贪心
题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...
- poj 1456 Supermarket(并查集维护区间)
题意:有一些货物,每一个货物有价值和卖出的截至日期,每天能够卖一个货物,问能卖出的最大价值是多少. 思路:算法不难想到,按价值降序排列.对于每一件货物,从deadline那天開始考虑.假设哪天空 ...
- POJ 1456 Supermarket(贪心+并查集)
题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...
- POJ 1456——Supermarket——————【贪心+并查集优化】
Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 1456 Supermarket(贪心+并查集优化)
一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2 10 1 20 2 10 1 50+20 50 2 40 ...
- poj1456(贪心+并查集)
题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...
- poj1456 Supermarket 贪心+并查集
题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...
随机推荐
- string用法总结
要想使用标准C++中的string类,必须要包含#include <string> 注意是<string>而不是<string.h>,带.h的是C语言中的头文件 s ...
- Linxu指令--date,cal
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- Oracle Start With关键字
Oracle Start With关键字 前言 旨在记录一些Oracle使用中遇到的各种各样的问题. 同时希望能帮到和我遇到同样问题的人. Start With (树查询) 问题描述: 在数据库中, ...
- java ecplise配置
运行java项目首先配置java运行时环境:Window->Preferences->Java->Installed JREs 修改为jdk:C:\Program Files\Jav ...
- android activity传递实体类对象
通过实现Parcelable接口序列化对象的步骤: 1.实现Parcelable接口.2.并且实现Parcelable接口的public void writeToParcel(Parcel dest, ...
- 小白成长系列--HTTP协议(一)
序:小白成长系列是笔者使用最简单易懂的逻辑来解释常见的计算机相关知识,不仅理解,还让你记忆深刻\(^o^)/ 先理解什么是协议? 协议就是双方要做某件事情而制定的规则,而且双方必须要遵从协议所约定的内 ...
- 锋利的jQuery笔记
首先分清jQuery对象和DOM对象,这两者可相互转化,如: var $cr=$("#cr"); //jquery对象 var cr=$cr[0] ; //DOM对象 var cr ...
- unix网络编程第2版(卷1)_第6章_同步_异步
第6章 I/O复用:select和poll函数 6.1概述 在5.12节中,我们看到TCP客户同时处理两个输入:标准输入和TCP套接口.我们遇到的问题是客户阻塞于(标准输入上的)fgets调用,而服务 ...
- ng机器学习视频笔记(十六) ——从图像处理谈机器学习项目流程
ng机器学习视频笔记(十六) --从图像处理谈机器学习项目流程 (转载请附上本文链接--linhxx) 一.概述 这里简单讨论图像处理的机器学习过程,主要讨论的是机器学习的项目流程.采用的业务示例是O ...
- VMware10不能安装64位(linux)系统,提示此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
今天下载VM10准备安装Ubuntu14.04,一如既往的进行安装,突然发现出现了问题:此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态,具体如图: 如图中提示可重启电脑进入B ...