题解 UVA1316 【Supermarket】
题目链接:
思路:
根据题目意思,我们需要用到贪心的思想,越晚过期的商品当然是越晚卖好。同时你假如有多个商品必须在同一天卖出,当然是卖收益多的好。
于是我们就有以下方法:首先将每个商品的过期时间按从小到大排序,同时建立一个小根堆,然后遍历这些商品。
如果商品过期时间大于堆中商品个数,说明前面还有些天是没有卖东西的,当然可以把这个商品插入堆中。
类似的,如果商品过期时间等于堆中商品个数,就比较此商品的收益和堆顶的收益哪个大。如果堆顶的收益小,就把他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】的更多相关文章
- UVA1316 Supermarket
题目描述 有一个商店有许多批货,每一批货又有N(0<=N<= 10^4104 )个商品,同时每一样商品都有收益 P_iPi ,和过期时间 D_iDi (1<= Pi,DiPi,D ...
- POJ1456 Supermarket 题解
思维题. 关键在于如何想到用堆来维护贪心的策略. 首先肯定是卖出的利润越大的越好,但有可能当前这天选定了利润最大的很久才过期而利润第二大的第二天就过期,这时的策略就不优了. 所以我们必须动态改变策略, ...
- 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 ...
- 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 ...
- Supermarket [堆]
Supermarket 题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=\(10^4\))个商品,同时每一样商品都有收益Pi ,和过期时间Di (1<=Pi,Di&l ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- kuangbin带你飞 并查集 题解
做这套题之前一直以为并查集是很简单的数据结构. 做了才发现自己理解太不深刻.只看重片面的合并集合.. 重要的时发现每个集合的点与这个根的关系,这个关系可以做太多事情了. 题解: POJ 2236 Wi ...
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
随机推荐
- window 定时任务小项目,每秒钟,每分钟访问url
个人百度网盘中 链接: https://pan.baidu.com/s/15sK2H-OtrAkAYcXjsxwihA 提取码: awv3 复制这段内容后打开百度网盘手机App,操作更方便哦
- 1.Json的学习--JSON.stringfy()
1.JSON.parse() JSON.parse() JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScr ...
- input 表单
1:datalist元素,一般与input组建配合使用,以定义可能输入的值,例如: <!DOCTYPE html> <html lang="en"> < ...
- ElementTree
http://effbot.org/zone/element-index.htm#installation http://effbot.org/zone/element-xpath.htm
- [CDH] Redis: Remote Dictionary Server
基本概念 一.安装 Redis: Remote Dictionary Server 远程字典服务 使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种 ...
- Cetos 7 命令行登陆与图形界面登陆相互切换
环境:vmware 虚拟机: 系统:Cetos 7 64位: 引言:有一台虚拟机,安装的时候选择的是最小化安装,是没有图形界面的,后来有需求,需要有个图形界面,所以就准备把这个升级下,下面是操作步骤: ...
- reduce过滤数组
原始数据 const data = [{ id: , spec: '规格1', rules: [{ rank: , breaks: }, { rank: , breaks: }, { rank: , ...
- v-for与v-if的优先级
原文地址 永远不要把 v-if 和 v-for 同时用在同一个元素上. 一般我们在两种常见的情况下会倾向于这样做: 为了过滤一个列表中的项目 (比如 v-for="user in users ...
- Python学习笔记——以函数为参数的内置函数
1.用法 一个参数 def ds(x): return 2 * x + 1 print(ds(5)) 11 g = lambda x : 2 * x + 1 print(g(5)) 11 两个参数 d ...
- windows下的句柄利用
什么是句柄 维基百科:在程序设计中,句柄(handle)是Windows操作系统用来标识被应用程序所建立或使用的对象的整数.其本质相当于带有引用计数的智能指针.当一个应用程序要引用其他系统(如数据库. ...