做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可。时间复杂度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 贪心+并查集的更多相关文章

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

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

  2. Supermarket POJ - 1456 贪心+并查集

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  3. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

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

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

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

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

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

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

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

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

  8. poj1456(贪心+并查集)

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

  9. poj1456 Supermarket 贪心+并查集

    题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...

随机推荐

  1. VAssistX插件

    一.什么是VassistX? VassistX的全称是Visual Assist X,是whole tomato开发的一个非常好用的插件,可用于VC6.0及Visual Studio的各个版本(包括V ...

  2. Java 线程和多线程执行过程分析

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  3. linux判断文件是否存在

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" # 这里的-x 参 ...

  4. .NET 异步多线程,Thread,ThreadPool,Task,Parallel,异常处理,线程取消

    今天记录一下异步多线程的进阶历史,以及简单的使用方法 主要还是以Task,Parallel为主,毕竟用的比较多的现在就是这些了,再往前去的,除非是老项目,不然真的应该是挺少了,大概有个概念,就当了解一 ...

  5. 【mac】mac os X更新High Sierra后出现的问题

    今天更新了一下macbook pro的系统到10.13.1版本,出现了几个小问题,总结一下解决方案: git客户端无法使用 解决方案如下: http://blog.csdn.net/kedongjun ...

  6. JAVA中的数据存储空间简述

    在 JAVA 中,有六个不同的地方可以存储数据: 1. 寄存器( register ): 最快的存储区,因为它位于不同于其他存储区——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进 ...

  7. wpf datagrid row height 行高自动计算使每行行高自适应文本

    wpf 的datagrid的行高 要么是Auto,要么是定值:但会带来麻烦就是每行行高都一样. 当需要按内容(主要是wrap 换行的textbox或textblock)来动态调整行高的时候,需要用到d ...

  8. MySQL索引基本应用[转]

    原文地址:http://www.php100.com/html/webkaifa/database/Mysql/2010/0409/4279.html 索引是快速搜索的关键.MySQL索引的建立对于M ...

  9. HDU 1384 Intervals &洛谷[P1250]种树

    差分约束 差分约束的裸题,关键在于如何建图 我们可以把题目中给出的区间端点作为图上的点,此处应注意,由于区间中被标记的点的个数满足区间加法,这里与前缀和类似,对于区间[L..R]来说,我们加入一条从L ...

  10. Centos启动默认打开网络

    Centos打开网络 测试的时候发现网络没有打开,得到图像界面点击网络打开.比较麻烦去搜索了解决方法在此记录下来. 通过 /etc/sysconfig/network-script/, 编辑ifcfg ...