POJ 1456 Supermarket(贪心+并查集)
题目链接:http://poj.org/problem?id=1456
题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了)。卖一件商品消耗一个单位时间,售卖顺序是可以改变的,求出最多可以卖多少钱。
解题思路:看了大牛的解释~。其实这道题是用贪心写的,这里并查集只是用来作为工具,使得速度更加快。贪心的写法是这样的,先把所有产品按照利润从大到小排序,然后这个把这个放在截止日期那天卖出,并做好标记,如果截至日期那天已经有其他产品占用了,那么可以把这个产品卖出的时间往前推,直到找到可以卖的那一天并标记好。其实直接写也可以,但是可以用并查集优化。 对于快速找到第一个不冲突的时间点呢,使用并查集很好地解决了这个问题。 这里并查集的作用类似于链表指针,压缩的过程就是删掉节点的过程。从而在O(1)的时间内找到那个不冲突的点。
代码1:普通贪心(127ms)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e4+; int vis[N]; struct node{
int dx,val;
bool operator <(const node &b)const{
return val>b.val;
}
}arr[N]; int main(){
int n,ans;
while(~scanf("%d",&n)){
ans=;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%d%d",&arr[i].val,&arr[i].dx);
}
sort(arr+,arr++n);
for(int i=;i<=n;i++){
for(int j=arr[i].dx;j>=;j--){
if(!vis[j]){
ans+=arr[i].val;
vis[j]=;
break;
}
}
}
printf("%d\n",ans);
}
return ;
}
代码2:贪心+并查集(47ms)
#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define LC(a) (a<<1)
#define RC(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=1e4+; int root[N]; int find(int x){
return x==root[x]?x:root[x]=find(root[x]);
} struct node{
int dx,val;
bool operator <(const node &b)const{
return val>b.val;
}
}arr[N]; int main(){
int n,ans;
while(~scanf("%d",&n)){
ans=;
for(int i=;i<N;i++){
root[i]=i;
}
for(int i=;i<=n;i++){
scanf("%d%d",&arr[i].val,&arr[i].dx);
}
sort(arr+,arr++n);
for(int i=;i<=n;i++){
int t=find(arr[i].dx);
if(t>){
ans+=arr[i].val;
root[t]=t-;
}
}
printf("%d\n",ans);
}
return ;
}
POJ 1456 Supermarket(贪心+并查集)的更多相关文章
- POJ 1456 Supermarket(贪心+并查集优化)
一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2 10 1 20 2 10 1 50+20 50 2 40 ...
- poj1456 Supermarket 贪心+并查集
题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
- nyoj 208 + poj 1456 Supermarket (贪心)
Supermarket 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 A supermarket has a set Prod of products on sal ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
- 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 - 并查集 - 贪心
题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...
- POJ 1456 (贪心+并查集) Supermarket
有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...
随机推荐
- BZOJ1901:Zju2112 Dynamic Rankings——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序 ...
- Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集
A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- [POI2007] ZAP-Queries (莫比乌斯反演)
[POI2007] ZAP-Queries 题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian S ...
- CCPC-Winter Camp div2 day5
DIV2 有部分div1的题会写 div1的大佬真的太强了 向他们学习 (好像和zqc大佬说过话了hhh,zqc大佬真的是一个超有意思的人啊,羡慕有妹子队友的zqc大佬) A: 你有一棵树,你想把它画 ...
- CursorAdapter中getView newView bindView异同
Adapter的作用是界面与数据之间的桥梁,通过设置适配器至ListView控件后(如调用ListView的 setAdapter(ListAdapter adapter) ...
- 移动端1px边框问题
用于手机端受dpr的影响,实际开发中,PC端和移动端展示的效果不太一样,往往在PC端显示的是1px,移动端常常是偏粗一些. 解决办法: 主要是用到伪类及缩放.在需要画边框的元素上,设置一个伪类,它的伪 ...
- Codeforces 351B Jeff and Furik 概率 | DP
B. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- python检测硬盘脚本
#!/usr/bin/env python # _*_coding:utf-8_*_ import os import sys import statvfs def main(): '''deamon ...
- 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法
poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...
- 使用TSQL语句操作MySQL数据库
使用TSQL语句创建数据库 以前用的是鼠标在界面上手动创建,这样创建会比较麻烦,而且还会经常出问题.在其它电脑上要用的话还需要重复操作.所以要使用程序代码操作,能通过代码的就不用手动操作. 在数据库界 ...