描述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

提示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.

题意:给n个商品,有利润pi和过期时间ti两个属性,每天只能卖出一件商品,问最大利润是多少?

题解:我们很容易想到要将商品按过期时间排列,但是如果再用利润作为第二维从大到小排列,然后从前往后选择,会导致状态的紊乱。

而我们知道,涉及排序以及贪心经常对其进行分类的讨论。

为了获得最大利润,我们每天都必须卖出一个物品,所以卖出物品的个数等于经过的天数。这样的话:

  • 我们建立一个大根堆
  • 将商品按照时间从小到大排列
  • 从第1个物品向后遍历,如果该物品刚好于上一天过期,则将该物品与该物品之前的利润最小的物品进行比较替换:
    • 如果当前物品的过期时间大于堆的大小(即经过的天数),直接将其放入堆中。
    • 如果当前物品的过期时间小于堆的大小(即经过的天数),这样的物品是不存在的,因为上一个卖出的物品的过期时间一定是大等于堆的大小的,而当前物品的过期时间大于等于堆的大小。
    • 如果当前物品的过期时间等于堆的大小(即经过的天数),与堆顶元素(利润最小)进行比较替换。
  • 堆中的所有物品即为卖出的物品,利润相加即可得到答案。
//#include<bits/stdc++.h>
#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include  <cassert>
#include   <cstdio>
#include   <vector>
#include   <string>
#include    <cmath>
#include    <queue>
#include    <stack>
#include      <set>
#include      <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<int,pair<int,int> >pii;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) {
	return b ? gcd(b, a%b) : a;
}
const int maxn=1e5+10;
int n;
pair<int,int>a[maxn];
priority_queue<int,vector<int>,greater<int> >q;

void retail() {
	for(int i = 1; i <= n; i++)scanf("%d%d", &a[i].se, &a[i].fi);
	sort(a+1,a+n+1);
	rep(i,1,n){
		if(a[i].fi==q.size()&&q.top()<a[i].se) {
			q.pop();
			q.push(a[i].se);
		}
		else if (a[i].fi>q.size()) q.push(a[i].se);//q.size()其实代表天数,为最大限度获得利润,每天都要卖 
	}
	int ans = 0;
	while (q.size()) {
		ans+=q.top();
		q.pop();
	}
	cout<<ans<<endl;
}
int main() {
	while (cin>>n) retail();
	return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注