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.并查集:并查集直 ...
随机推荐
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 【vue】index.html main.js app.vue index.js怎么结合的? 怎么打包的?搜集的信息
转载:https://blog.csdn.net/yudiandemingzi/article/details/80247137 怎么结合的: 一.启动项目 第一步:cmd进入项目文件里,运行npm ...
- delphi 事务处理SQL语句
方法一(利用adoconnection.exe(sqlstate)): adoconnection1.begintrans;//开始事务try adoconnection1.execute(sqlst ...
- 【C++】不要在构造函数或析构函数内调用虚函数
这个问题来自于<Effective C++>条款9:永远不要在构造函数或析构函数中调用虚函数 . 假设有如下代码: class Transaction {// 所有交易的基类 public ...
- python判断字符串是否包含子字符串
python的string对象没有contains方法,不可以使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数 python的st ...
- 第89天:HTML5中 访问历史、全屏和网页存储API
一.访问历史 API 通过history对象实现前进.后退和刷新之类的操作 history新增的两个方法history.replaceState()和history.pushState()方法属于HT ...
- Now or later UVALive - 3211(2-SAT 最小值最大化)
emmm...去吃早饭了... rujia讲的很好.. 最小值最大化问题,,,二分枚举答案 设x1.x2为同一个集合中的元素,y1.y2为另一个集合中的元素,如果x1与y1之差小于mid,那么如果 ...
- 一些noip模拟题一句话题解
Problem A: 序列 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 12 Solved: 9[Submit][Status][Web Boar ...
- 【BZOJ5251】【八省联考2018】劈配(网络流,二分答案)
[BZOJ5251][八省联考2018]劈配(网络流,二分答案) 题面 洛谷 BZOJ Description 一年一度的综艺节目<中国新代码>又开始了. Zayid从小就梦想成为一名程序 ...
- BZOJ4070 [Apio2015]雅加达的摩天楼 【分块 + 最短路】
题目链接 BZOJ4070 题解 考虑暴力建图,将每个\(B_i\)向其能到的点连边,复杂度\(O(\sum \frac{n}{p_i})\),当\(p\)比较小时不适用 考虑优化建图,每个\(dog ...