Supermarket(贪心/并查集)
原创的博客
题意:
超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润。
每天只能卖一个商品。
现在你要让超市获得最大的利润。
n , p[i], d[i] 范围都在10000以内 。
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const ll mod=;
const int INF= 0x3f3f3f3f;
const int N=4e5+; int n; priority_queue<int>q;
vector<int>v[N]; int main()
{
while(cin>>n)
{
for(int i=;i<=;i++) v[i].clear() ;
priority_queue<int>empty;
swap(empty, q); //相当于清空q。 for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[y].push_back(x);
} int ans=;
for(int i=;i>=;i--)
{
for(int j=;j<v[i].size();j++)
{
q.push(v[i][j]);
}
if(!q.empty())
{
ans+=q.top(); q.pop();
}
}
printf("%d\n",ans);
}
}
用另一种贪心的方法来做,先把所有产品按照利润从大到小排序,然后这个把这个放在截止日期那天卖出,并做好标记,如果截至日期那天已经有其他产品占用了,那么可以把这个产品卖出的时间往前推,直到找到可以卖的那一天并标记好。 按照这种思路提交,AC了,不过却用了141 ms。
用了这个方法之后,再回想了下并查集方法的代码, 所谓的用并查集做,实际上是对上面那种方法的优化!
用并查集的关键之处是,我们知道按照上面那个方法,假设一个产品a占用了一个日期后,那么如果下次又有一个产品b和产品a的截止日期是相同的,但是那个日期以被占用了,所以就要往前移动1天,那么就可以用并查集进行标记,在a占用了那个日期后,把a的截止日期指向前一个日期,这样的话,可以直接查找到他要占用到哪一个时间。 用了并查集优化后,时间为47MS。
---------------------
作者:shuangde800
来源:CSDN
原文:https://blog.csdn.net/shuangde800/article/details/8022068
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const ll mod=;
const int INF= 0x3f3f3f3f;
const int N=4e5+; int n; //vector<int>v[N];
int vis[N];
struct node
{
int p,d;
}a[N];
bool cmp(node x,node y)
{
return x.p>y.p || x.p==y.p&&x.d>y.d;
} int main()
{
while(cin>>n)
{
for(int i=;i<=;i++) vis[i]=;
int maxT=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].p,&a[i].d);
if(maxT<a[i].d) maxT=a[i].d;
}
sort(a+,a++n,cmp);
int ans=;
for(int i=;i<=n;i++)
{
if(!vis[a[i].d ])
{
vis[a[i].d ]=;
ans+=a[i].p;
}
else{
for(int j=a[i].d; j>=;j--)
{
if(!vis[j])
{
vis[j]=;
ans+=a[i].p;
break;
}
}
}
}
cout<<ans<<endl;
}
}
无并查集
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const ll mod=;
const int INF= 0x3f3f3f3f;
const int N=4e5+; int n;
int f[N]; struct node
{
int p,d;
}a[N];
bool cmp(node x,node y)
{
return x.p>y.p;
} int getf(int x)
{
if(x!=f[x])
{
f[x]=getf(f[x]);
}
return f[x];
}
int main()
{
while(cin>>n)
{
for(int i=;i<=;i++) f[i]=i; int maxT=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].p,&a[i].d);
if(maxT<a[i].d) maxT=a[i].d;
}
sort(a+,a++n,cmp);
int ans=;
for(int i=;i<=n;i++)
{
int fa=getf(a[i].d);
if(fa>)
{
ans+=a[i].p;
f[fa]=fa-;
}
}
cout<<ans<<endl;
}
}
+并查集
Supermarket(贪心/并查集)的更多相关文章
- poj1456 Supermarket 贪心+并查集
题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...
- POJ 1456 Supermarket(贪心+并查集优化)
一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2 10 1 20 2 10 1 50+20 50 2 40 ...
- POJ 1456 Supermarket(贪心+并查集)
题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- POJ 1456——Supermarket——————【贪心+并查集优化】
Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 1456 (贪心+并查集) Supermarket
有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...
- poj1456(贪心+并查集)
题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
随机推荐
- Python的网页解析库-PyQuery
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...
- 本地win下JConsole监控远程linux下的JVM
环境:服务器端: Linux + jdk1.7.0_75 + tomcat 7本地: Win + jdk1.7.0_55 一.修改/etc/hosts文件 hostname -i 如果显示127.0. ...
- LeetCode 912. 排序数组(Sort an Array) 43
912. 排序数组 912. Sort an Array 题目描述 每日一算法2019/6/15Day 43LeetCode912. Sort an Array
- MATLAB自定义函数
MATLAB自定义函数形式 function [a,b,c] = funname(x1,x2,x3) 输入变量 对于输入变量,MATLAB可以识别输入变量的个数,通过nargin来记录当前输入变量个数 ...
- 【转帖】LSM树 和 TSM存储引擎 简介
LSM树 和 TSM存储引擎 简介 2019-03-08 11:45:23 长烟慢慢 阅读数 461 收藏 更多 分类专栏: 时序数据库 版权声明:本文为博主原创文章,遵循CC 4.0 BY-S ...
- 嵌入式02 STM32 实验07 串口通信
STM32串口通信(F1系列包含3个USART和2个UART) 一.单片机与PC机串行通信研究目的和意义: 单片机自诞生以来以其性能稳定,价格低廉.功能强大.在智能仪器.工业装备以及日用电子消费产品中 ...
- git 学习笔记 --多人协作
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ git r ...
- CSDN刷阅读数
今天我们来盘一下csdn,做一个小程序,为什么做这个呢?今天小编看着我的博客的阅读数,唉,惨不忍睹,没办法,只能想一些........呃呃呃呃,你懂的. 话不多说,分析一波csdn的阅读数,计数原理是 ...
- Entity framework Core 数据库迁移
本文转自https://www.cnblogs.com/zmaiwxl/p/9454177.html 初始化数据库 1.添加初始迁移 Add-Migration init 向“迁移”目录下的项目添加以 ...
- CSP J/S 2019受虐记
一枚蒟蒻的游记~ 提高组DAY1 不是说每场考试都有一道签到题吗 那我tm读了三遍题硬是没找到一道水题是怎么回事(是我太弱了吗) 没办法,硬着头皮做T1 暴力写法...期望得分30pts 于是...在 ...