题目链接

描述

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

分析:

给定一些商品本身的价值和保质期,只要在保质期内的任意商品都可以出售,但是如果超过保质期就不能够出售了,要求的就是这些商品说能获得的最大的价值。

既然要求最大价值肯定就用到了贪心的思想,原先就是只用贪心写,时间超。这还要加入一个并查集,其主要作用就是能够快速的找出当前商品的出售日期(这样比for循环一个一个往下找要快)。

商品排序的话肯定就是按照价值从达到小排序了,并不用管他的保质期,因为只要把价值最大的商品都出售了,当然也就获得最大的价值了。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int parent[10009];
int Max=-1;
struct Node
{
int value;
int day;
} node[10009]; bool cmp(Node a,Node b)
{
return a.value>b.value;
} void init( )///parent数组初始化
{
for(int i=0; i<=10005; i++)
parent[i]=i;
} int Find(int x)///找当保质期限为x的物品,应该在第几天卖出
{
if(x==parent[x])
return x;
else
return parent[x]=Find(parent[x]);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
{
scanf("%d%d",&node[i].value,&node[i].day);
}
sort(node,node+n,cmp);
init();
long long int sum=0;
int a;
for(int i=0;i<n;i++)
{
a=Find(node[i].day);///当前这个物品应该在第几天卖
{
if(a!=0)///它可以在保质期之内卖出去
{
sum+=node[i].value;
parent[a]=Find(a-1);///也就相当于其他的保质期为a的物品不能在第a天卖出去了,要提前一天卖
}
}
}
printf("%lld\n",sum);
}
return 0;
}

NYOJ 208 Supermarket (模拟+并查集)的更多相关文章

  1. 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)

    题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树.  开始和队友 ...

  2. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. HDU 2860 (模拟+并查集)

    Regroup Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

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

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

  6. nyoj 1022 合纵连横 经典并查集

    思路:关键在于并查集的删点操作. 给每个诸侯国一个另外的编号,比如box[i]表示诸侯国i现在处于第box[i]个联盟,可以随时改变它的联盟编号,并且让box[i] = k, 实现删除操作.以前联盟中 ...

  7. poj1456 Supermarket 贪心+并查集

    题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...

  8. NYOJ 1022 合纵连横 (并查集)

    题目链接 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法吞并那些实力弱的,让自己的领土面积不断扩大.而实力弱的诸侯王为了不让自己的领 ...

  9. POJ_1456 Supermarket 【并查集/贪心】

    一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...

随机推荐

  1. Java Map 在put值时value值不被覆盖

    一.问题描述 最近在代码开发中遇到一个问题,在往Map中put文件路径值然后把Map放到List中去时,遇到问题是Map的后一个值总是把前一个值覆盖,导致最后Map中只有一个值. 二.解决办法(有如下 ...

  2. Maven 私服安装和启动

    在安装私服的时候容易碰到的两个问题,一个是安装时拒绝访问,另一个是安装完成后服务无法启动: 拒绝访问问题: 原因:没有以管理员身份运行 cmd 解决办法: 如果是 win7 的话,可以直接在 [运行- ...

  3. spring学习 8-面试(事务,解决线程安全)

    1.介绍一下Spring的事物管理 参考:Spring 学习7 -事务 2.Spring如何处理线程并发问题    Spring使用ThreadLocal解决线程安全问题 参考:Spring学习11- ...

  4. delphi self 的使用

    delphi之self 在使用delphi的对象技术的时候,经常会看到一个词汇:self,它到底指的是什么呢? 我们还要从对象与类的关系谈起. 类是对将要创建的对象的性质的描述,是一种文档.这很重要: ...

  5. 【C++】深度探索C++对象模型读书笔记--Data语意学(The Semantics of data)

    1. 一个空类的大小是1 byte.这是为了让这一类的两个对象得以在内存中配置独一无二的地址. 2. Nonstatic data member 放置的是“个别的class object”感兴趣的数据 ...

  6. 【uoj#192】[UR #14]最强跳蚤 Hash

    题目描述 给定一棵 $n$ 个点的树,边有边权.求简单路径上的边的乘积为完全平方数的点对 $(x,y)\ ,\ x\ne y$ 的数目. 题解 Hash 一个数是完全平方数,当且仅当每个质因子出现次数 ...

  7. BZOJ3620 似乎在梦中见过的样子(kmp)

    不是很懂为什么数据范围要开的这么诡异,想到正解都不敢写.用类似NOI2014动物园的方法,对每个后缀求出类似next的数组即可. #include<iostream> #include&l ...

  8. CF1100E

    i207M给的题 省选前-小题解合集 给定一张有向图,每条边有边权.你可以花费边权的代价反转一条边,使得原图中没有环.最小化反转的边权的最大值. 首先二分,然后考虑判定. 转化为有些边可以翻转,有些边 ...

  9. C++堆和栈详解(转)

    一.预备知识—程序的内存分配    一个由C/C++编译的程序占用的内存分为以下几个部分    1.栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等.其    操 ...

  10. poj1850 Code

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10059   Accepted: 4816 Description ...