• 题目链接:

    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;
}

最新文章

  1. 【NLP】Python NLTK 走进大秦帝国
  2. PHP计算一年有多少周,每周开始日期和结束日期
  3. ZOJ3772 - Calculate the Function(线段树+矩阵)
  4. Asm Shader Reference --- Shader Model 2.x part
  5. virtualenv创建虚拟环境
  6. 04737_C++程序设计_第3章_函数和函数模板
  7. python 时间处理
  8. git 生成公钥、私钥方法与clone使用方法
  9. 服务器cpu100%问题分析
  10. iptables 学习
  11. is,as,类库
  12. iOS之Safari调试webView/H5页面
  13. SpringMVC.入门篇.一.HelloWorld
  14. 通过nginx + lua来统计nginx上的监控网络请求和性能
  15. Canvas中的剪切clip()方法
  16. JAVAEE学习——hibernate01:简介、搭建、配置文件详解、API详解和CRM练习:保存客户
  17. JAVA多线程------用1
  18. Navicat for MySQL导入文件
  19. MySQL 实现字符串换行
  20. CF1060D Social Circles

热门文章

  1. css3属性clip
  2. 11 MySQL之性能优化
  3. 14 statefulset (sts)控制器
  4. 图片滚动js代码
  5. PCD(点云数据)文件格式
  6. 连接局域网mysql数据库
  7. springboot-helloworld-idea
  8. python-Web-django-路由保护
  9. CentOS7安装openjdk8+环境变量配置
  10. 【并行计算-CUDA开发】CUDA线程、线程块、线程束、流多处理器、流处理器、网格概念的深入理解