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 ...
随机推荐
- Linux 性能检查常用命令
####消耗CPU最多的进程 [root@Yong ~]# ;|head ##拼接进程号 [root@Yong ~]# |awk '{print $1}' |awk BEGIN{RS=EOF}'{gs ...
- Core Location :⽤用于地理定位
Core Location :⽤用于地理定位 在移动互联⽹网时代,移动app能解决⽤用户的很多⽣生活琐事,⽐比如 导航:去任意陌⽣生的地⽅方 周边:找餐馆.找酒店.找银⾏行.找电影院 在上述应⽤用中, ...
- iOS面试题总结(持续更新)
过段时间打算跳槽,找了一些面试题来做,在这里做个总结方便review,希望能对要面试的童鞋有帮助. 以下为面试题: 运行以下代码会有什么结果 NSString *str1 = @"str1& ...
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- Hbase学习指南
本篇Hbase组件基于CDH5进行安装,安装过程:https://www.cnblogs.com/dmjx/p/10037066.html Hbase简介 HBase是一个高可靠.高性能.面向列.可伸 ...
- python学习之字符串转换
配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- def strCase() ...
- python -- sftp的方式下载终端文件
可以通过paramiko模块进行远程连接,然后指定文件夹,进行下载. sf = paramiko.Transport((host, port) #创建链接对象,需要终端ip以及sftp使用的端口号 ...
- C语言实例解析精粹学习笔记——26
实例26:阿拉伯数字转换为罗马数字,将一个整数n(1~9999)转换为罗马数字,其中数字和罗马数字的对应关系如下: 原书中的开发环境很老,我也没有花心思去研究.自己在codeblocks中进行开发的, ...
- ZOJ3329 概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- 2 semantic ui 框架的应用
为什么使用css框架 1.使用基础样式 : ui segment 分段:内容片段 <link rel="stylesheet" href="css/semanti ...