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.

 前言如上一篇博客

本题还是一道并查集

先按照权值排序

权值高的,时间长的在前

对每个商品从ddl往前找空余的时间(类比找祖先节点)

如果没用则不用

因此将时间看成点

被卖了就连接前一个点

直到前面没有点。

众所周知链状的和暴力遍历没有区别

所以记得路径压缩。

#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
using namespace std;	
const int N=1e5;
int par[N]; //存储每个点的祖宗节点
//int rank[N]; //树的高度
// 返回x的祖宗节点
struct XD{
	int v,t;
};
XD z[N];
int cmp(XD a,XD b){
	if(a.v==b.v) return a.t>b.t;
	else return a.v>b.v;
}
void init(int n)
{
	for (int i = 1; i <=n; i ++ ) 
	{
		par[i] = i;// 初始化
		//rank[i]=0;
	}
}
int find(int x)
{
    if (par[x] == x) 
	{
		return x;
	}
	else
	{
		return par[x]=find(par[x]);
	}
}
void unite(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x==y) return ;
	par[x]=y;
	//if(rank[x]<rank[y])
	//{
	//	par[x]=y;
	//}
	//else
	//{
	//	par[y]=x;
	//	if(rank[x]==rank[y])return rank[x]++;
	//}
}
bool same(int x,int y)
{
	return find(x)==find(y);
}
int main()
{
	int n,m;
	while(cin>>n)
	{
		int ans=0;
		init(10000);
		for(int i=1;i<=n;i++)
		{
			cin>>z[i].v>>z[i].t;
		}
		sort(z+1,z+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(find(z[i].t)>0)
			{
				par[find(z[i].t)]--;
				ans+=z[i].v;
			}
		}
		cout<<ans<<"\n";
		
	}
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值