Description

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. 

Input

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.

Output

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.

Sample Input

4  50 2  10 1   20 2   30 1

7  20 1   2 1   10 3  100 2   8 2
5 20 50 10

Sample Output

80
185

Hint

The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

【题意】给出n的商品,给出各自的价格和截止日期,求最大价值

【思路】按价格排序,把价格高的商品安排在截止日期,若已经安排了商品,往前寻找,直到全满。

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
struct node
{
int p,d;
} a[N];
int vis[N];
bool operator <(node a,node b)
{
return a.p>b.p;
}
int main()
{
int n,maxd,sum;
while(~scanf("%d",&n))
{
maxd=;
memset( vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
scanf("%d%d",&a[i].p,&a[i].d);
maxd=max(maxd,a[i].d);
}
sort(a+,a++n);
sum=;
for(int i=; i<=n; i++)
{
if(!vis[a[i].d])//该截止日期是否已标记,若没有就安排在这天
{
vis[a[i].d]=;
sum+=a[i].p;
}
else//若已经标记,往前面的日期找,是否能找到空的
{
for(int j=a[i].d-; j>=; j--)
{
if(!vis[j])
{
sum+=a[i].p;
vis[j]=;
break;
}
}
} }
printf("%d\n",sum);
}
return ;
}

Supermarket_贪心的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. OID,主键生成策略,PO VO DTO,get和load区别,脏检查,快照,java对象的三种状态

    主键生成策略 sequence 数据库端 native 数据库端 uuid  程序端 自动赋值 生成的是一个32位的16进制数  实体类需把ID改成String 类型 assigned  程序端 需手 ...

  2. S2 第三章SQL编程

    .if练习 --统计并显示2013-- 的oop考试平均分 --如果平均分在70以上,显示“考试成绩优秀”,并显示前三名学生的考试信息 --如果在70分以下,显示“考试成绩较差”,并显示后三名学生的考 ...

  3. A New Tetris Game

    时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte 总提交: 40            测试通过: 12 描述 曾经,Lele和他姐姐最喜欢,玩得最 ...

  4. Xcode 8 打包上线 iTunes Connect 找不到构建版本

    Xcode 8 打包上线 iTunes Connect 找不到构建版本 最近苹果推出新的mac操作系统(macOS Sierra 10.12),大家可能都已经升级了,作为一个开发者,小编肯定是第一时间 ...

  5. while、do while练习——7月24日

    while循环的格式是for循环的变形 //while 循环(当循环),是for循环的变形 //for(int i=0;i<=5;i++) //{ // Console.WriteLine(&q ...

  6. I-MooFest(POJ 1990)

    MooFest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5697   Accepted: 2481 Descripti ...

  7. ios基础篇(二)——UIImageView的常见用法

    UIImageView是在界面上显示图片的一个控件,在UIImageView中显示图片的话应该首先把图片加载到UIImage中,然后通过其他方式使用该UIImage. 创建UIImageView有两种 ...

  8. oracle个人总结

    oracle优化原则 1:insert 插入 (1):insert into /*+ append */ NOLOGGING 2: select 查询 (1):/*+ full(v) */ 全表查询 ...

  9. LZMA demo挑选使用备忘

    一.源码下载:http://sourceforge.net/projects/sevenzip/ 二.各种EXE及其功能 代码里面有很多的dsw工程,功能不一,size不一,需要挑选合适的加入到自己的 ...

  10. shell 下的$符合

    $n        $1 the first parameter,$2 the second...$#        The number of command-line parameters.$0  ...