题目链接

描述

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. 学生导师互选系统(php代码规范)

    学生导师互选系统(php代码规范) php编码规范 组名:一不小心就火了 负责项目:学生导师互选系统(安卓端) 编写目的 为了更好的提高团队的的合作效率,保证开发的有效性和合理性,并可最大程度的提高程 ...

  2. [学习]WireShark 的过滤功能

    1. 打开 wireShark 过滤显示 协议 比如显示arp协议 过滤栏输入arp即可 支持的协议类型 TCP UDP HTTP FTP ICMP SMTP等等 2. 过滤ip地址 ip.addr ...

  3. session的基本原理

    转载:http://blog.sina.com.cn/s/blog_8155e74d0101iqmh.html 如何严格限制session在30分钟后过期! 1.设置客户端cookie的lifetim ...

  4. 我的 MyBatis 实现的 Dao 层

    学了 Mybatis 之后,发现用 Mybatis 写 Dao层实在是简便多了,主要是在表的映射这块简单了很多.下面是我实现的使用 Mybatis 实现的简单的操作用户表的 Dao 层. 使用 Myb ...

  5. Contest 7

    A:搜索好难啊根本不会啊. B:原题都能写挂没救了啊.考虑求出每个数作为最小值时能向左向右扩展到的最远位置,那么这段区间里的所有数就不可能作为唯一的最小值成为最优解了,否则假设可以的话这段区间里的数都 ...

  6. 题解 P1420 【最长连号】

    这个题过去的同学可以再来一题(P1567 统计天数): https://www.luogu.org/problemnew/show/P1567 是的,这个题其实也不是很难,就是前后比较,将天数压栈, ...

  7. 第三周——构建一个简单的Linux系统MenuOS

    [洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周  构建一个 ...

  8. Spark集群基础概念 与 spark架构原理

    一.Spark集群基础概念 将DAG划分为多个stage阶段,遵循以下原则: 1.将尽可能多的窄依赖关系的RDD划为同一个stage阶段. 2.当遇到shuffle操作,就意味着上一个stage阶段结 ...

  9. python基础----析构函数__del__

    析构方法,当对象在内存中被释放时,自动触发执行. 注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构 ...

  10. winform布局 FlowLayoutPanel的控件

    http://www.cnblogs.com/moon-mountain/archive/2011/09/08/2171232.html 1.采用流布局:工具箱里边容器里有一个:FlowLayoutP ...