• 题目链接:

    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. Qt Model/View 的简单说明

    目录: (一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型 ...

  2. Tomcat安装应用部署及配置文件解读

    Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomcat和Nginx,APa ...

  3. coursera 视频总是缓冲或者无法观看的解决办法(Windows 和 Linux 系统 环境)

    现在读了一个机器学习方向的博士,虽然这么长时间也没有学明白什么,但是没事的时候也会看看一些书籍和资料,学这个方向的人基本都会看过吴恩达的coursera课程上的机器学习课程,我也是如此,不过交了钱以后 ...

  4. Django View视图

    视图view 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者 ...

  5. Python中使用第三方库xlrd来写入Excel文件示例

    Python中使用第三方库xlrd来写入Excel文件示例 这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一样,xlrd表示read xls,xlwt表 ...

  6. JAVA解析_操纵_JS_JAVA_JS引擎

    http://uuhorse.iteye.com/blog/1705684 http://blog.csdn.net/chinajash/article/details/1491570 http:// ...

  7. pyinstaller发布exe,弹出Failed to execute script main

    1.在PyCharm中按Alt+F12打开Terminal对话框 1.1我的项目文件放在wxpython目录下,D:\learn\Weather index insurance\wxpython> ...

  8. sudo: unable to execute ./xxx.py: no such file or directory

    $ ./mk_dataset.py -bash: ./mk_dataset.py: /home/ocean1101/anaconda3/bin/python^M: bad interpreter: N ...

  9. Prometheus + Grafana 监控 Redis

    Prometheus安装 .linux-amd64.tar.gz .linux-amd64. cd /prometheus # Start Prometheus. # By default, Prom ...

  10. postman 请求接口 Could not get any response

    前提: 今天用postman请求接口的时候,能请求到接口,但是打断点后发现方法里面要抛出异常就出现错误: 错误原因: 返回的 http 的 code不是3位的,如下我写成了四位 4002: <? ...