Supermarket

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σx∈Sellpx. An optimal selling schedule is a schedule with a maximum profit.

For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80.

Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products.

输入
A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.
输出
For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.
样例输入
4  50 2  10 1   20 2   30 17  20 1   2 1   10 3  100 2   8 25 20  50 10
样例输出
80
185

解题思路:这里用贪心的思想,对物品按照价值从大到小排序,然后每次选择比当前时间小的时间点,若不存在可用时间则不选。

最开始的想法是用dp,结果看错题意了,目前还没有想到dp的解法。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;const int maxn=1e4+100;
struct node
{int pri;int time;bool operator <(const node& a)const{if(pri==a.pri)return time<a.time;return pri>a.pri;}
}a[maxn];
int n;
bool vis[maxn];int main()
{while(scanf("%d",&n)!=EOF){memset(vis,0,sizeof(vis));for(int i = 0; i < n; i++)scanf("%d %d",&a[i].pri,&a[i].time);sort(a,a + n);int ans = 0;for(int i = 0; i < n; i++)for(int j = a[i].time; j >= 1; j--)if(!vis[j]){vis[j] = 1;ans += a[i].pri;break;}printf("%d\n",ans);}return 0;
}

nyoj 208 Supermarket(贪心)相关推荐

  1. Supermarket | 贪心

    Supermarket | 贪心 from poj 1456 from acwing 145 时间限制 :2s 内存限制:65M Description: A supermarket has a se ...

  2. Supermarket | 贪心 + 并查集

    Supermarket | 贪心 + 并查集 from poj 1456 from acwing 145 时间限制 :2s 内存限制:65M Description: A supermarket ha ...

  3. NYOJ - 非洲小孩(贪心)

    http://nyoj.top/problem/1036 内存限制:64MB 时间限制:1000ms 题目描述: 家住非洲的小孩,都很黑.为什么呢? 第一,他们地处热带,太阳辐射严重. 第二,他们不经 ...

  4. poj 1456 Supermarket (贪心, 并查集)

    链接: http://poj.org/problem?id=1456 题目: Description A supermarket has a set Prod of products on sale. ...

  5. poj 1456 Supermarket 贪心+并查集(个人感觉有点难判断出来

    poj 1456 这第一眼还觉得只要贪心就可以了,但是emmm看了大佬的题解居然真的要用到并查集= = 大佬清晰的思路 大佬舒服的代码 #pragma warning(disable:4996) #i ...

  6. UVa1316 Supermarket(贪心)

    问题:超市里有 n个商品出售,每个商品如果在d截止时间内出售,可以获取利润p.每个商品出售需要一个时间单位.问最大利润是多少? 思路: 第一种方法是根据截止时间升序排列,同时依赖优先级队列,当商品的截 ...

  7. NYOJ 287 Radar 贪心之 区间选点

    Radar 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Assume the coasting is an infinite straight line. Land ...

  8. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  9. 训练指南第一部分解题报告

    主要是提供训练指南第一部分解题报告链接,后面会持续更新中 307 - Sticks  (DFS+剪枝) 11292 - Dragon of Loowater (贪心) 11729 - Commando ...

最新文章

  1. [转载] 晓说——第18期:古代科举考试那些事——招生
  2. 井下三专两闭锁的内容_矿用高压防爆开关:煤矿井下高低压防爆开关五防电子锁保护装置...
  3. 从PHP安全讲DedeCms的安全加固
  4. Java异常知识整理_处理异常时的性能开销
  5. mybatisplus查询今天的数据_MybatisPlus(CRUD)
  6. VTK:一个面向对象的可视化类库(zz)
  7. VC++的dll中接收消息
  8. iPhone 14 Pro系列传出好消息,有望实现8GB内存自由
  9. [Be a Coding Plasterer] Components 1:get Basic Things
  10. 深度神经网络训练过程中为什么验证集上波动很大_一个值得深思的问题?为什么验证集的loss会小于训练集的loss...
  11. JAVA_JSP教师信息管理系统
  12. 【JAVA】poi-tl简单实现调查问卷word模板
  13. java会员消费管理系统源码_基于jsp的会员消费商场vip管理系统-JavaEE实现会员消费商场vip管理系统 - java项目源码...
  14. 数组取交集、并集与补集
  15. 雅虎首席产品官Blake Irving:打造个性化产品的“架子鼓手”
  16. linux rar文件怎么解压_Linux下rar压缩包的解压方法
  17. 单反相机坏点和噪点测试软件,单反相机验机及坏点测试方法(多数适用于佳能)...
  18. 向量旋转(或矢量旋转或坐标轴旋转)后xy坐标重定位(vivado+cordic ip核+matlab) - 适用于数学爱好者
  19. vue实现拖拽的组件
  20. Selenium一些特殊情况的处理:失去焦点、点击不生效、长页面处理、先触发事件才能动态加载的元素、日期输入

热门文章

  1. 250鲁大师跑分_鲁大师跑分20万起步的闲鱼二手电脑能买么?只要四招轻松告别套路...
  2. linux 电源管理 regulator,Linux内核电源管理综述
  3. lwip1.4.1需要的文件
  4. WebApi 的CRUD 的方法的应用
  5. 趣味Python入门(一):初识Python
  6. mysql多实例配置使用方法
  7. linux 进程地址空间的一步步探究
  8. 百度搜索,你画了好大一张饼,你还打算欺骗多少人呢?有图为证
  9. H3C交换机典型访问控制列表(ACL)配置实例
  10. WIN32开发:如何获取父进程的ID