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.
Input
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.
Output
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.
Sample Input
4 50 2 10 1 20 2 30 1
7 20 1 2 1 10 3 100 2 8 2
5 20 50 10
Sample Output
80
185
Hint
The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.
大概题意:给你一个商品 和商品必须在 t 时间内完成销售
求在所有给定的时间中最大的时间 t 内最多能销售多少
虽然是一个并查集的题 但是感觉就是贪心好想一些 大时间里小的也一定能完成销售 就把满足条件的放进队列里 就每次在队列里选最大价值的销售
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
const int N=2e5+5;
using namespace std;
struct node
{
int px,dx;
} a[N];
bool cmp(node x,node y)
{
return x.dx>y.dx;//所需时间按大到小排列
}
priority_queue<int,vector<int>,less<int> >q;//队列优先级 价值大的先跳出
int main()
{
int n;
while(cin>>n)
{
int maxn=-1;
for(int i=0; i<n; i++)
{
cin>>a[i].px>>a[i].dx;
maxn=max(maxn,a[i].dx);//找出所有商品中所需时间最大的
}
sort(a,a+n,cmp);
while(!q.empty())
q.pop();
int pos=0,ans=0;
for(int t=maxn; t>0; --t)//每销售一个商品 时间就减1
{
while(pos<n&&a[pos].dx>=t)//销售期限比t大的物品都可以在t时刻销售
q.push(a[pos++].px);
if(!q.empty())//每次销售一个商品
{
ans+=q.top();
q.pop();
}
}
cout<<ans<<endl;
}
return 0;
}
再想了一下 这个应该怎么去贪呢?每看一件商品,我们尽可能的让现在的这个物品再他的截至时间销售,这样后面再有比他截至时间小的 我们也许可以把后面那个给销售掉;
先排序
价值100 50 20 10 8 5 2
时间 2 10 1 3 2 20 1
t=2时销售价值为100的物品;
t=10时销售价值为50的商品;
t=1时销售价值为20的商品;
t=3时销售价值为10的商品;
再看第五个物品就有点问题了 他的截至时间是第二天 很显然如果要销售这个第五个商品,只能再第一天和第二天销售,然而第一天和第二天都有物品要销售且这两天要销售的物品都比第五个商品的价值要大,那我们就舍弃他,继续看下面的物品;
t=20时销售价值为5的商品;
第七个商品同第五个;
蹬蹬蹬
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=2e5+5;
struct node
{
int px,dx;
} a[N];
bool cmp(node x,node y)
{
return x.px>y.px;//将物品按照价值从大到小排列;
}
int vis[N];//标记该时间点是否已经销售物品
int main()
{
int n;
while(cin>>n)
{
int maxn=-1;
for(int i=0; i<n; i++)
{
cin>>a[i].px>>a[i].dx;
maxn=max(maxn,a[i].dx);
}
sort(a,a+n,cmp);
int ans=0;
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
{
if(!vis[a[i].dx])//如果该时间点没有销售东西
{
ans+=a[i].px;
vis[a[i].dx]=1;
}
else//如果该时间点已销售的了物品
//但是我们又想把剩下物品里价值最大的物品销售掉,就只能找比该物品截至时间小的时间点销售
for(int j=a[i].dx-1; j>=1; j--)
if(!vis[j])
{
ans+=a[i].px;
vis[j]=1;
break;
}
}
cout<<ans<<endl;
}
return 0;
}