NYOJ 208 Supermarket (模拟+并查集)
描述
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 (模拟+并查集)的更多相关文章
- 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)
题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树. 开始和队友 ...
- 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 ...
- HDU 2860 (模拟+并查集)
Regroup Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- POJ 1456 Supermarket(贪心+并查集优化)
一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2 10 1 20 2 10 1 50+20 50 2 40 ...
- nyoj 1022 合纵连横 经典并查集
思路:关键在于并查集的删点操作. 给每个诸侯国一个另外的编号,比如box[i]表示诸侯国i现在处于第box[i]个联盟,可以随时改变它的联盟编号,并且让box[i] = k, 实现删除操作.以前联盟中 ...
- poj1456 Supermarket 贪心+并查集
题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...
- NYOJ 1022 合纵连横 (并查集)
题目链接 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法吞并那些实力弱的,让自己的领土面积不断扩大.而实力弱的诸侯王为了不让自己的领 ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
随机推荐
- beta-1 阶段各组员的贡献分分配
小组名称:飞天小女警 项目名称:礼物挑选小工具 小组成员:沈柏杉(组长).程媛媛.杨钰宁.谭力铭 bera-1阶段各组员的贡献分分配如下: 姓名 团队贡献分 谭力铭 5.2 沈柏杉 5.1 程媛媛 4 ...
- 利用vs10和opencv识别图片类型身份证的号码
遇到的问题: 1 持续灰色图像框 waitkey()要在imshow()之前调用. 2 CvRect 和Rect CvXXX是C语言的接口,cv::XXX是C++语言的接口.两者混在一起容易出错 3 ...
- BZOJ3620 似乎在梦中见过的样子(kmp)
不是很懂为什么数据范围要开的这么诡异,想到正解都不敢写.用类似NOI2014动物园的方法,对每个后缀求出类似next的数组即可. #include<iostream> #include&l ...
- P2774 方格取数问题
题目背景 none! 题目描述 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于 ...
- ans_rproxy 说明
ans_rproxy 说明 网络IP资源分配 Windows2008R2: IP: 172.16.204.50/24 Gateway: 172.16.204.1 ...
- 【BZOJ2109/2535】【NOI2010】航空管制(贪心)
[BZOJ2109/2535][NOI2010]航空管制(贪心) 题面 BZOJ2109 BZOJ2535 题解 很好玩的一道题目 先看第一问,显然是要找一个合法的拓扑排序的序列. 直接拓扑排序,把队 ...
- CF449,急需提高姿势水平
这可能是我第1场只出1题的div1? A 鬼畜题? 我的dev没有字符统计功能,于是只好扔到notepad++上.数错好几次.题目本身没什么特别的地方. B 当时一直没想出来QAQ 题目中说m> ...
- USB驱动之CDC类的介绍与应用20160905
USB的协议其实是很复杂的,如果要深入学习估计要一两年才能熟悉透.本文主要是讲如何使用官方已经写好的库进行二次开发,以达到我们自己使用的目的.我们知道USB可以用来接U盘,声卡,读卡器,鼠标键盘等等, ...
- 很好的c++和Python混合编程文章
c++中嵌入python入门1 本人是用vc2003+python2.5学习的,其它的也应该差不了多少 0. 坏境设置把Python的include/libs目录分别加到vc的include/lib ...
- poj3421 X-factor Chains
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7733 Accepted: 2447 D ...