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. EFCode First 导航属性

    首先谈谈自己对EF的接触的过程吧,最先接触EF只是因为EF支持从数据库把关系扒下来,可以省掉自己写Select.Update.Insert这些SQL语句,而且修改非常方便,后来在使用的过程中发现导航属 ...

  2. 一个简单的excel文件上传到数据库方法

    因为以前项目中有用到过Excel导入,所以整理了一下,这是一个导入Excel数据到数据库的方法 注意:需要导入poi jar包 代码清单 /** * Excel 导入 * @param mapping ...

  3. CCF 2016-12 送货

    问题描述 试题编号: 201512-4 试题名称: 送货 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 为了增加公司收入,F公司新开设了物流业务.由于F公司在业界的良好口碑, ...

  4. 一模 (5) day2

    第一题: 题目大意:使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少? n<=2*10^9 解题过程: 1.以前看到过这题了,一个数x的位数=(int)lg(x)+1  换一下底就是 ...

  5. 一模 (3) day1

    第一题: 题目大意:给出m个小于n的数,求出出现次数大于m div 2 的数. 1<=n<=2^31   1<=m<=10000 解题过程: 1.看到m的数据范围比较小,直接  ...

  6. BeanUtils组件

    引入jar包(需要引入依赖的日志jar包) Person p = new Person(); p.setName("Daisy"); p.setAge(12); //对象的copy ...

  7. Spring学习笔记之整合struts

    1.现有项目是通过 <action    path="/aaaaAction"                type="org.springframework.w ...

  8. 合理利用 vs2013的性能分析跟诊断

    选择对应的项目==> 我正常是选择采样 就包括里面的一些耗时.  挺好用的. 可以根据热路径 还有访问的占比.知道哪个环节占用的访问时间 还有性能耗能多. 可以点进去 跟踪跟修改

  9. elementoryOS / ubuntu U盘启动问题的解决

    具体现象: 进入U盘启动后,停顿在"start booting from usb device..."不动. 解决方法:  将syslinux文件夹下的syslinux.cfg中的 ...

  10. 将table导出为Excel的标准无乱码写法

    导出为Excel有很多种写法,对于一些复杂的格式,笔者喜欢在后台先拼成一个<table>,再使用Response输出. 如果数据中包含中文或者一些特殊字符,可很多不规范的写法都会导致页面乱 ...