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.

用贪心的思想,从后面开始卖,时间越后面的越后面卖,然后后面卖不完,但是比前面贵的可以拿来前面卖,然后运算最后面时间的就好了,不到O(n^2)复杂度,不会超时

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define cl clear()
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
const double pi=acos(-1.0);
typedef __int64 ll;
typedef long double ld;
const ll mod=1e9+7;
using namespace std;
int a[10005],b[10005];
int main()
{
int n;
while(sf("%d",&n)!=EOF)
{
mm(a,0);mm(b,0);int max=0;
for(int i=1;i<=n;i++)
{
sf("%d%d",&a[i],&b[i]);
if(b[i]>max)max=b[i];
}
int sum=0;
for(int i=max;i>0;i--)
{
int ans=0;
for(int j=1;j<=n;j++)
{
if(b[j]>=i)
{
if(a[j]>a[ans])
ans=j;
}
}
sum+=a[ans];
b[ans]=0;
}
pf("%d\n",sum);
}
return 0;
}

G - Supermarket的更多相关文章

  1. G - Supermarket poj1456

    题目的描述很长,其实描述的问题很简单,说有n的商品,它们每个的价值是pi,但是呢,再过di天这些商品就不能卖了(有可能过期了...),现在给出来每个商品的价值和可以卖的最后期限,问可以得到最多多少资金 ...

  2. kuangbin 并查集

    A : Wireless Network  POJ - 2236 题意:并查集,可以有查询和修复操作 题解:并查集 #include<iostream> #include<cstdi ...

  3. [kuangbin带你飞]专题五 并查集

    并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...

  4. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  5. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  6. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  7. Codeforces 815 C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  8. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  9. [转]Linux下g++编译与使用静态库(.a)和动态库(.os) (+修正与解释)

    在windows环境下,我们通常在IDE如VS的工程中开发C++项目,对于生成和使用静态库(*.lib)与动态库(*.dll)可能都已经比较熟悉,但是,在linux环境下,则是另一套模式,对应的静态库 ...

随机推荐

  1. 实录分享 | 计算未来轻沙龙:揭秘AutoML技术(视频 + PPT)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/c9Yv2cf9I06K2A9E/article/details/83542784 10 月 27 日 ...

  2. LiteIDE 在 Windows 下为 Go 语言添加智能提示代码补全

    本文以 Windows 7 64 位为环境,go1.4.2.windows-amd64 和 liteidex27.2.1.windows-qt5 为例. 成功搭建开发环境后,发现 LiteIDE 没有 ...

  3. Centos7找不到ifconfig和netstat命令

    Centos7发布有一段时间了,最近使用中也发现一些问题,从Centos6换过来后感觉到不少细微的变化 例如默认没有ifconfig和netstat两个命令了,ifconfig其实使用ip addr命 ...

  4. 学习下知然网友写的taskqueue

    博主在他的博客里对taskqueue的各种使用情况和使用方法都介绍的很清楚:http://www.cnblogs.com/zhiranok/archive/2013/01/14/task_queue. ...

  5. [AaronYang原创] 大话ASP.NET MVC3+ (C#与DOM与JS页面上的很炫的技巧)

    案例一:比如我要传递一个新闻标题和新闻内容 存储到数据库去 1.最简单的通用Url公式就是 function getUrl() { var d = $("#表单ID").seria ...

  6. sringboot项目在tomcat上的部署

    sringboot项目在tomcat上的部署原文链接: https://blog.csdn.net/zhaoyahui_666/article/details/78283559#comments 20 ...

  7. 使用Python解析JSON详解

    为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. JSON 函数 ...

  8. java证书

    默认情况下,密钥项存储在.keystore文件中,而可信的CA证书项存储在.cacerts文件中,该文件位于JRE安全目录中. 想在Linux环境下,用keytool命令检查一下一个证书,打keyto ...

  9. pandas数组(pandas Series)-(5)apply方法自定义函数

    有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...

  10. linux每日命令(5):mkdir命令

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录名或路径名 2. ...