题目描述

有一个商店有许多批货,每一批货又有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 提供的翻译

题目描述

PDF

输入输出格式

输入格式:


输出格式:

输入输出样例

输入样例#1:

4 50 2 10 1 20 2 30 1
7 20 1 2 1 10 3 100 2 8 2 5 20 50 10
输出样例#1:

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的更多相关文章

  1. 题解 UVA1316 【Supermarket】

    题目链接: https://www.luogu.org/problemnew/show/UVA1316 思路: 根据题目意思,我们需要用到贪心的思想,越晚过期的商品当然是越晚卖好.同时你假如有多个商品 ...

  2. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. Supermarket POJ - 1456

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...

  4. 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 ...

  5. 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 ...

  6. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  7. G - Supermarket

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...

  8. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  9. CF815C Karen and Supermarket

    题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...

随机推荐

  1. mpvue项目中安装weui

    观察一下发现,mpvue项目打包css的规律是:根组件App.vue里的style样式全部打包到 dist  /  static / css / app.wxss ..   参照微信小程序的原生引入使 ...

  2. c#常用数据结构解析【转载】

    引用:http://blog.csdn.net/suifcd/article/details/42869341 前言:可能去过小匹夫博客的盆油们读过这篇对于数据结构的总结,但是小匹夫当时写那篇文章的时 ...

  3. C++ 基础 初始化列表

    当一个类组合了其他类,或者使用了 const 成员,就要用 初始化列表. Class A {...}; Class B {...}; Class C { private: A a; B b; int ...

  4. 008---Django的模版层

    python的模板:HTML代码+模板语法 <!--模版语法之变量--> <h1>Index </h1> <p>{{ name }}</p> ...

  5. mysql学习第三天笔记

    连接连接是在多个表之间通过一定的连接条件,使表之间发生关联,进而能从多个表之间获取数据.在 WHERE子句中书写连接条件. 如果在多个表中出现相同的列名,则需要使用表名作为来自该表的列名的前缀. N个 ...

  6. saltstack plug in

    目录 可插拔的子系统 灵活性 虚拟模块 salt的核心架构提供了一种高速的交流总线,在核心架构的上层,salt暴露出来的特征是:松散耦合,可插拔的子系统. 可插拔的子系统 salt包含20中插件系统, ...

  7. python datetime offset-aware与offset-navie相互转换

    python datetime offset-aware与offset-navie相互转换 2016年11月13日 16:20:43 阅读数:2393 有时,我们使用python 的datetime模 ...

  8. TCP重组问题

    今天问题: vqmon 测试一pcap抓包文件18.pcap.发现实际输出的视频分片信息和抓包不符合. ===>pts : 00:00:33 Too much data in TCP recei ...

  9. Java中String类new创建和直接赋值字符串的区别

    转自:https://blog.csdn.net/a986410589/article/details/52454492 方式一:String a = “aaa” ; 方式二:String b = n ...

  10. java编程-无锁初始化

    private final Node<K,V>[] initTable() { Node<K,V>[] tab; int sc; while ((tab = table) == ...