• 题目链接:

    https://www.luogu.org/problemnew/show/UVA1316

  • 思路:

    根据题目意思,我们需要用到贪心的思想,越晚过期的商品当然是越晚卖好。同时你假如有多个商品必须在同一天卖出,当然是卖收益多的好。

    于是我们就有以下方法:首先将每个商品的过期时间按从小到大排序,同时建立一个小根堆,然后遍历这些商品。

    如果商品过期时间大于堆中商品个数,说明前面还有些天是没有卖东西的,当然可以把这个商品插入堆中。

    类似的,如果商品过期时间等于堆中商品个数,就比较此商品的收益和堆顶的收益哪个大。如果堆顶的收益小,就把他POP出堆顶,然后将此商品插入到堆中;相反,如果堆顶的收益大,那就不进行操作。

  • 代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
using namespace std;
const int maxn=10008;
struct Goods{
int p,d;
}s[maxn];
struct Small_Heap{
int n,heap[maxn];
inline void up(int fa){
while(fa>1){
if(heap[fa]<heap[fa>>1]){
swap(heap[fa],heap[fa>>1]);
fa=fa>>1;
}
else break;
}return ;
}
inline void insert(int v){
heap[++n]=v;
up(n);
return ;
}
inline void down(int fa){
int s=fa<<1;
while(s<=n){
if(s<n && heap[s]>heap[s+1])s++;
if(heap[s]<heap[fa]){
swap(heap[s],heap[fa]);
fa=s,s=fa<<1;
}
else break;
}return ;
}
inline void extract(){
heap[1]=heap[n--];
down(1);
}
inline int GetTop(){
return heap[1];
}
}a;
inline bool cmp(const Goods &a,const Goods &b){
return a.d<b.d;
}
template<class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;
return ;
}
int main()
{
int n,x;
while(cin>>n){
int ans=0; //堆中商品个数
for(register int i=1;i<=n;i++){
read(s[i].p),read(s[i].d);
}
sort(s+1,s+1+n,cmp);
// for(register int i=1;i<=n;i++)cout<<s[i].p<<' '<<s[i].d<<endl;
for(register int i=1;i<=n;i++){
if(s[i].d==a.n){
if(s[i].p>a.GetTop()){
a.extract();
a.insert(s[i].p);
}
}
else if(s[i].d>a.n){
a.insert(s[i].p);
}
}
for(register int i=1;i<=a.n;i++){
// cout<<a.heap[i]<<endl;
ans+=a.heap[i];
a.heap[i]=0;
}a.n=0;
printf("%d\n",ans);
}
return 0;
}

题解 UVA1316 【Supermarket】的更多相关文章

  1. UVA1316 Supermarket

    题目描述 有一个商店有许多批货,每一批货又有N(0<=N<= 10^4104 )个商品,同时每一样商品都有收益 P_iPi​ ,和过期时间 D_iDi​ (1<= Pi,DiPi,D ...

  2. POJ1456 Supermarket 题解

    思维题. 关键在于如何想到用堆来维护贪心的策略. 首先肯定是卖出的利润越大的越好,但有可能当前这天选定了利润最大的很久才过期而利润第二大的第二天就过期,这时的策略就不优了. 所以我们必须动态改变策略, ...

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

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

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

  5. CF815C Karen and Supermarket

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

  6. Supermarket [堆]

    Supermarket 题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=\(10^4\))个商品,同时每一样商品都有收益Pi​ ,和过期时间Di​ (1<=Pi,Di&l ...

  7. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

  8. kuangbin带你飞 并查集 题解

    做这套题之前一直以为并查集是很简单的数据结构. 做了才发现自己理解太不深刻.只看重片面的合并集合.. 重要的时发现每个集合的点与这个根的关系,这个关系可以做太多事情了. 题解: POJ 2236 Wi ...

  9. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. APScheduler 3.0.1浅析

    简介 APScheduler是一个小巧而强大的Python类库,通过它你可以实现类似Unix系统cronjob类似的定时任务系统.使用之余,阅读一下源码,一方面有助于更好的使用它,另一方面,个人认为a ...

  2. Winform运行外部控制台程序,并在程序结束后执行其他动作

    ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = @"程序名"; psi.Arguments = @&qu ...

  3. linux下的开源NFC协议栈

    1. 协议栈名称 neardal 2. 源码 https://github.com/connectivity/neardal.git 3. 由谁维护? intel 4. 基于neardal的nfc协议 ...

  4. redis多实例与主从同步及高级特性(数据过期机制,持久化存储)

    redis多实例 创建redis的存储目录 vim /usr/local/redis/conf/redis.conf #修改redis的配置文件 dir /data/redis/ #将存储路径配置修改 ...

  5. mybatis之动态SQL操作之查询

    1)  查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层 * @author AdminTC */ public class StudentDao { /** * ...

  6. egg源码浅析一npm init egg --type=simple

    要egg文档最开始的时候,有这样的几条命令: 我们推荐直接使用脚手架,只需几条简单指令,即可快速生成项目: $ mkdir egg-example && cd egg-example ...

  7. 各种集合key,value能否为null

    转: 各种集合key,value能否为null 2019年03月12日 13:22:58 mingwulipo 阅读数 238   HashMap key,value都可以为null static f ...

  8. Oracle数据库导入(expdp)和导出(impdp)

    文档最后,列出了常用的一些导入导出的场景,以及一些导入导出的属性说明. 一.数据库导出(expdp) 使用sys或system账号登录oracle 通过"Window + R" 打 ...

  9. Oracle动态执行脚本创建序号

    -----------------------动态创建序列的脚本--------------------------- declare type num_list ) ); -- 老师表.学生表 xz ...

  10. Product - 产品经理 - 内容

    特别说明 本文是已读书籍的学习笔记和内容摘要,原文内容有少部分改动,并添加一些相关信息,但总体不影响原文表达. - ISBN: 9787568041591 - https://book.douban. ...