POJ1456 Supermarket —— 贪心 + 路径压缩优化
题目链接:http://poj.org/problem?id=1456
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13736 | Accepted: 6206 |
Description
For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80.

Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products.
Input
Output
Sample Input
4 50 2 10 1 20 2 30 1 7 20 1 2 1 10 3 100 2 8 2
5 20 50 10
Sample Output
80
185
Hint
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; struct node
{
int p, d;
bool operator<(const node &x)const{
return p>x.p;
}
}a[MAXN];
int used[MAXN], n; int main()
{
while(scanf("%d", &n)!=EOF)
{
memset(used, , sizeof(used));
for(int i = ; i<=n; i++)
scanf("%d%d", &a[i].p, &a[i].d);
sort(a+, a++n); int ans = ;
for(int i = ; i<=n; i++)
{
for(int j = a[i].d; j>=; j--)
if(!used[j])
{
used[j] = ;
ans += a[i].p;
break;
}
} printf("%d\n", ans);
} }
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; struct node
{
int p, d;
bool operator<(const node &x)const{
return p>x.p;
}
}a[MAXN];
int used[MAXN], fa[MAXN], n; int find(int x) { return (fa[x]==-)?x:fa[x]=find(fa[x]); } int main()
{
while(scanf("%d", &n)!=EOF)
{
memset(used, , sizeof(used));
memset(fa, -, sizeof(fa));
for(int i = ; i<=n; i++)
scanf("%d%d", &a[i].p, &a[i].d);
sort(a+, a++n); int ans = ;
for(int i = ; i<=n; i++)
{
int j = find(a[i].d);
if(j>)
{
fa[j] = j-;
ans += a[i].p;
}
} printf("%d\n", ans);
}
}
POJ1456 Supermarket —— 贪心 + 路径压缩优化的更多相关文章
- poj1456 Supermarket 贪心+并查集
题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...
- 并查集路径压缩优化 UnionFind PathCompression(C++)
/* * UnionFind.h * 有两种实现方式,QuickFind和QuickUnion * QuickFind: * 查找O(1) * 合并O(n) * QuickUnion:(建议使用) * ...
- POJ1456 Supermarket 贪心
贪心策略:一定先卖价值最大的,然后考虑卖当前的物品,卖的日期越靠后,越优,可以为以后的物品提供机会 #include <stdio.h> #include <string.h> ...
- POJ-1456 Supermarket 贪心问题 有时间限制的最小化惩罚问题
题目链接:https://cn.vjudge.net/problem/POJ-1456 此题与HDU-1789完全是一道题 题意 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售 ...
- 关于并查集的路径压缩(Path Compress)优化
之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...
- HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)
6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...
- Google Pagespeed,自动压缩优化JS/CSS/Image
Google Pagespeed,自动压缩优化JS/CSS/Image 浏览: 发布日期:// 分类:技术分享 关键字: Nginx Appache Pagespeed 自动压缩优化JS/CSS/Im ...
- Apache配置压缩优化时报错——undefined symbol: inflateEnd
Apache配置压缩优化时报错——undefined symbol: inflateEnd 环境:CentOS 6.4 软件版本:httpd-2.4.6 apr-1.4.8 apr-util-1.5. ...
- 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction
http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...
随机推荐
- Codeforces 868F Yet Another Minimization Problem(分治+莫队优化DP)
题目链接 Yet Another Minimization Problem 题意 给定一个序列,现在要把这个序列分成k个连续的连续子序列.求每个连续子序列价值和的最小值. 设$f[i][j]$为前 ...
- 教你写Linux设备驱动程序:一个简短的教程
教你写Linux设备驱动程序:一个简短的教程 http://blog.chinaunix.net/uid-20799298-id-99675.html
- django使用logging记录日志
django使用logging记录日志,我没有用这方式去记录日志,主要还是项目小的原因吧, 有机会遇见大项目的话可以回头研究. 配置setting.py配置文件 import logging impo ...
- P2P技术简介(包括BT软件的分析)(转)
这是一篇别人发表的论文,里面很全面的解释了P2P技术的实现,以及BT网络中应用P2P技术所设计的原理,并列举BT软件的一些专业名词的定义.由于论文发表的比较早,2005年时还没有DHT技术. (链接: ...
- Maven的POM简单理解
以下引用自官方的POM介绍https://maven.apache.org/guides/introduction/introduction-to-the-pom.html: 一.什么是POM? 项目 ...
- mac下mysqldump找不到命令
之所以会出现MySQL或者mysqldump这样的命令找不到, 我们可以打开/usr/bin文件夹,发现bin目录中并没有mysql打头的UEF文件, 而在/usr/local/mysql/bin中可 ...
- win10 sublime license
—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- [反汇编练习] 160个CrackMe之032
[反汇编练习] 160个CrackMe之032. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- eclipse下的ssh框架整合过程及測试
最近在搭建Stuts2+hibernate+spring的框架,网上看的教程,大部分都是非常easy的步骤.没有比較具体的步骤以及每一个步骤完毕之后怎样检查是否配置成功.下面是笔者依据自己搭建的过程进 ...
- 并行程序设计---cuda memory
CUDA存储器模型: GPU片内:register,shared memory: host 内存: host memory, pinned memory. 板载显存:local memory,cons ...