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 ...
随机推荐
- JQuery 过滤选择器 与属性修改的方法演示比较
文本匹配 在表单输入项里面输入值,根据输入值,点击判断按钮,让对应的复选框选中 <html> <head> <meta http-equiv="Content- ...
- 【学时总结】 ◆学时·II◆ IDA*算法
[学时·II] IDA*算法 ■基本策略■ 如果状态数量太多了,优先队列也难以承受:不妨再回头看DFS-- A*算法是BFS的升级,那么IDA*算法是对A*算法的再优化,同时也是对迭代加深搜索(IDF ...
- poj_2689_Prime Distance
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- 搭建私有maven库发布及使用流程
一:背景 Apache Maven是当Java技术栈前最流行的项目管理工具,它提供了一系列方便快捷的命令帮助程序员们进行Java工程的开发工作.Maven服务器位于美国,由于出国带宽和众多因素,在国内 ...
- maven入门2
1.修改maven本地仓库位置 没有效果,在新建项目时还是转跳到默认配置 在默认仓库位置添加修改后的setting文件,失败 修改成功,原因是前面修改的是setting而我们需要修改的是default ...
- hadoop-2.0.0-cdh4.1.2升级到hadoop-2.7.2
升级前准备: 如果是 centos6.x的系统得升级glibc和pam包 在/etc/ld.so.conf 文件里添加 /usr/src/jdk1.6.0_23/jre/lib/amd64/serve ...
- linux系统批量创建用户和生成8位随机密码
1.1 脚本案例 批量创建20个用户,用户名为user1-user20,并生成8位随机登录密码,用户组采用键盘输入的方式,并将用户名及登录密码保存到/tmp/create_user.txt普通文 ...
- 【转摘】TFS上分支和标签的用法
引用路径:http://blog.csdn.net/cxzhq2002/article/details/8518250 什么时候用分支: 例如为某个客户定制的专用版本,和主干的特性有很大差别.不具通 ...
- 【JavaScript高级程序设计】6.1 理解对象
上一章曾经介绍过,创建自定义对象的最简单方式就是创建一个Object 的实例,然后再为它添加属性和方法,如下所示. var person = new Object(); person.name = & ...
- http与www服务精解
TCP/IP 协议介绍 在介绍 HTTP 协议之前,先简单说一下TCP/IP协议的相关内容.TCP/IP协议是分层的,从底层至应用层分别为:物理层.链路层.网络层.传输层和应用层,如下图所示: 从应用 ...