UVA1316 Supermarket
题目描述
有一个商店有许多批货,每一批货又有N(0<=N<= 10^4104 )个商品,同时每一样商品都有收益 P_iPi ,和过期时间 D_iDi (1<= Pi,DiPi,Di <= 10^4104 ),一旦超过了过期时间,商品就不能再卖。
你要做的就是求出每批货最多能得到多少收益。
输入输出格式
输入格式
多组数据,每组先给出一个整数N,表示这批货的商品个数。
然后有N对数,每对数有两个用空格隔开的正整数 P_i,D_iPi,Di ,表示第i个商品的收益和过期时间。相邻两对数之间用空格隔开。
输入以一个文件终止符结束,并且保证所有输入数据正确。
输出格式
对于每组数据,输出一行一个整数表示该批货物能卖出的最大价格。
感谢@Rye_Catcher 提供的翻译
题目描述
输入输出格式
输入格式:
输出格式:
输入输出样例
4 50 2 10 1 20 2 30 1
7 20 1 2 1 10 3 100 2 8 2 5 20 50 10
80
185
Solution:
本题贪心。。。
一个很简单的想法就是尽可能的让价值大的先卖,并且尽可能地在时间快要超过限制时卖(很显然,这样能给前面提供更多的选择)。
但是这样的话是$n^2$的,多组数据有点虚。
于是,我们换汤不换药,改成用堆去动态维护。
先将物品按限制时间从小到大排序,再以价值为关键字建立小根堆,然后判断(设$tot$为堆中元素个数):
1、若$t[i].d>tot$,说明当前只用了$tot$天,选$t[i]$不会过期,则直接将$t[i]$加入堆中。
2、若$s[i].d==tot$,说明当前用了$tot$天,选$t[i]$恰好过期,所以与堆顶的元素价值比较,若堆顶价值$<t[i].p$则弹出堆顶将$t[i]$入堆,否则就不操作。
最后输出堆中元素价值之和就好了。
代码:
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)>(b)?(b):(a))
using namespace std;
const int N=,inf=;
int n,m;
struct node{
int p,d;
bool operator <(const node &a) const {return p>a.p;}
}t[N];
priority_queue<node>q; il bool cmp(const node &a,const node &b){return a.d<b.d;} il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return f?-a:a;
} int main(){
while(scanf("%d",&n)==){
For(i,,n) t[i].p=gi(),t[i].d=gi();
sort(t+,t+n+,cmp);
int tot=,ans=;
For(i,,n)
if(t[i].d>tot)q.push(t[i]),tot++;
else if(t[i].d==tot) {
if(t[i].p>q.top().p)q.pop(),q.push(t[i]);
}
while(tot--)ans+=q.top().p,q.pop();
printf("%d\n",ans);
}
return ;
}
UVA1316 Supermarket的更多相关文章
- 题解 UVA1316 【Supermarket】
题目链接: https://www.luogu.org/problemnew/show/UVA1316 思路: 根据题目意思,我们需要用到贪心的思想,越晚过期的商品当然是越晚卖好.同时你假如有多个商品 ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- Supermarket POJ - 1456
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...
- codeforces 815C Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- codeforces round #419 E. Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- Codeforces 815C Karen and Supermarket 树形dp
Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...
- G - Supermarket
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
- CF815C Karen and Supermarket
题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...
随机推荐
- BZOJ3170: [Tjoi2013]松鼠聚会(切比雪夫距离转曼哈顿距离)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1524 Solved: 803[Submit][Status][Discuss] Descripti ...
- >题解< 校门外的树
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是 11 米.我们可以把马路看成一个数轴,马路的一端在数轴 00 的位置,另一端在 LL 的位置:数轴上的每个整数点,即 0,1 ...
- Atlas实现mysql主从分离
可以接受失败,无法接受放弃!加油! 一.介绍Atlas及架构图 Atlas源代码用C语言编写,它对于Web Server相当于是DB,相对于DB相当于是Client,如果把Atlas的逻辑放到Web ...
- c# 解决读取Excel混合文本类型,数据读取失败的解决方法
错误重现: ----------------------------------------------------------------------- 在导入Excel读取数据时,其中的一个字段保 ...
- redis安装与简单使用
第一步 新建一个文件 第二步 利用winscrp软件从本机上传redis的压缩包到linux新建的rdtar目录 第三步 cd rdtar 第四步 解压 tar zxvf redis-2+t ...
- 处理nginx访问日志,筛选时间大于1秒的请求
#!/usr/bin/env python ''' 处理访问日志,筛选时间大于1秒的请求 ''' with open('test.log','a+',encoding='utf-8') as f_a: ...
- jupyter notebook中出现ValueError: signal only works in main thread 报错 即 长时间in[*] 解决办法
我在jupyter notebook中新建了一个基于py3.6的kernel用来进行tensorflow学习 但是在jupyter notebook中建立该kernel时,右上角总是显示 服务正在启动 ...
- select into from 和 insert into select
select into from 和 insert into select都是用来复制表, 两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建. inser ...
- POJ 1180 斜率优化DP(单调队列)
Batch Scheduling Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4347 Accepted: 1992 ...
- [BZOJ1060][ZJOI2007]时态同步(树形DP)
[我是传送门] 因为边权只能增加,那么设f[u]为u子树上从i出发到达某个叶节点的最大路径, 显然Ans应该增加f[u]-f[v]-e[i].w Code #include <cstdio> ...