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 1

7  20 1   2 1   10 3  100 2   8 2
5 20 50 10
样例输出
80
185
 /**
题目大意:
求:能够得到的最大利润
数据说明:
n ==> 表示有n种商品
px dx ==> 只要商品在dx (包括dx)之前买完就可以获得px的利润
一天只可以买一种商品 步骤(贪心):
①、将px降序排列
②、每件商品从最大期限的时间的开始查找直到找到可以使用的时间 (或者到第一天)
②(1)如果是找到满足条件的时间就将该件商品的px加入能够得到的总利润中(然后跳出循环)
②(2)如果是找不到就不将该商品的px加入到总利润中
**/

核心代码:

 sort (P, P+n, cmp);

 for (int i = ; i < n; ++ i)
{
for (int j = P[i].dx; j >= ; -- j)
{
if (!book [i])
{
book [i] = ;
cnt += P[i].px;
break;
}
}
}

C/C++代码实现(AC):

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream> using namespace std; int n, cnt, flag[*]; struct node
{
int v, k;
}P[*]; bool cmp(node a, node b)
{
return a.v > b.v;
} int main(){
while(~scanf("%d", &n))
{
cnt = ;
memset(flag, , sizeof(flag));
for(int i = ; i < n; ++ i)
scanf("%d%d", &P[i].v, &P[i].k);
sort(P, P + n, cmp);
for(int i = ; i < n; ++ i)
{
for(int j = P[i].k; j >= ; -- j)
{
if (!flag[j])
{
flag[j] = ;
cnt += P[i].v;
break;
}
}
}
printf("%d\n", cnt);
}
return ;
}

nyoj 208 + poj 1456 Supermarket (贪心)的更多相关文章

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

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

  2. POJ 1456 Supermarket(贪心+并查集优化)

    一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2  10 1   20 2   10 1    50+20 50 2  40 ...

  3. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. POJ 1456——Supermarket——————【贪心+并查集优化】

    Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  5. POJ 1456 Supermarket(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...

  6. poj 1456 Supermarket(贪心+优先队列)

    题目链接:http://poj.org/problem?id=1456 题意:有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售处,就能得到相应的利润,并且销售该商品需要1天时间. ...

  7. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  8. Supermarket POJ - 1456(贪心)

    题目大意:n个物品,每个物品有一定的保质期d和一定的利润p,一天只能出售一个物品,问最大利润是多少? 题解:这是一个贪心的题目,有两种做法. 1 首先排序,从大到小排,然后每个物品,按保质期从后往前找 ...

  9. POJ 1456(贪心)

    #include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...

随机推荐

  1. LInux下npm install 安装失败问题

    现象: 今天公司自己动部署的Jenkins出现了问题,在执行npm install的时候,失败了,下载不到npm,在查阅了各种报错信息之后还是没有解决,发现用淘宝镜像进行安装时,也会有安装不成功的情况 ...

  2. stm32cubeMX配置LWIP

    MCU:stm32f769NIHx  PHY:LAN8742A LWIP_VERSION:2.0.3 1.配置RCC,串口(printf debug log) (1)开启RCC,配置时钟系统 图1.1 ...

  3. ubuntu16.04安装zlib

    sudo apt-get install zlib1g-dev 下载:libzip-1.0.1.tar.gztar zxcv libzip-1.0.1.tar.gzcd libzip-1.0.1./c ...

  4. SVG系列 - 基础

    标题为SVG基础,但是过于基础的东西就不再熬述啦,可以参考几个学习网址: SVG参考手册:http://www.runoob.com/svg/svg-reference.html MDN SVG:ht ...

  5. 热烈祝贺达孚电子(NDF)网站上线

    尊敬的客户: 您们好! 为适应公司发展的需要,树立公司的良好形象,满足大家更多的了解电容器系列产品及公司的服务,经过1个多月的筹备,在2019年10月21日公司网站正式上线啦,这标志着NDF(达孚电子 ...

  6. 代码传奇 | 身价10亿的程序员 雷军当年也为他打工——WPS之父 求伯君

    他的前半生,值得我们每一个人深思. 在普通人眼里,他寂寂无名,只有年岁稍长的文化人,才听说过他传奇般的存在. 在IT人眼里,他是块活化石,中国第一的大旗除了他,没人敢抗! 他是求伯君,从一个浙江穷山村 ...

  7. Linux命令比较文件内容

    文件准备 创建两个文件,分别为a.txt和b.txt,它们所含内容分别为: a.txt b.txt 1-wfhune2-chdamnsbchj3-uyr92fiubkqw5-cgvdnsb 2-djy ...

  8. Java多线程编程(五)定时器Timer

    一.定时器Timer的使用 在JDK库中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务.Timer类的主要作用就是设置计划任务,但封装任务的类确实TimerTask类,执行计 ...

  9. (六)添加adbmingling

    给环境变量Path添加adb命令路径,即adb.exe所在的目录 C:\Users\LIU Liang\AppData\Local\Android\Sdk\platform-tools

  10. 游图邦YOTUBANG是如何搭建生态系统的?

    现在的我们最关心的一个问题就是任何一个行业,如果没有办法很好的落地,就算描绘的非常美好,那也只是空中楼阁.昙花一现而已,它无法实现长久的一个发展.互联网时代呢,就是一个流量为王的一个时代,谁拥有庞大的 ...