UVA 1316 - Supermarket(贪心+经典问题)

A supermarket has a set Prod of products on sale. It earns a profit px for each product x in 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 from Prod such that the selling of each product x in Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is UVA 1316 - Supermarket(贪心+经典问题). An optimal selling schedule is a schedule with a maximum profit.

For example, consider the products Prod={a,b,c,d} with (pa,pa=(50,2), (pb,pb=(10,1), (pc,pc=(20,2), and (pd,pd=(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.

UVA 1316 - Supermarket(贪心+经典问题)

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

题意:有一些商品,每个商品在ti时刻之前能卖,每个商品价值为vi,每次卖一个商品要花1时间,问最大能卖出价值。

思路:本题n最大1W,t最大1W,下面三种方法分别适用不同情况。

1.贪心+优先队列。先按时间大的排序,从最大时间t往前找,把当前任务时间ti大于t的任务入队。队列按价值大的优先,在当前时间取出价值大的复杂度O(nlogn),适用于n很大,t很小的情况

2.也是贪心+优先队列。先按时间小的排序。然后每个任务入队前,判断队列中有多少元素,就代表时间从那时候开始可以用,如果大于就入队,如果小于就和队列中价值最小的交换。复杂度O(nlogn),适用于n很大,t很小的情况

3.贪心+并查集。从价值最大的去卖,每次往前找一个空位,空位利用并查集快速查找,复杂度O(nlogn)。适用于上述两种情况

多亏坑爹的UVA,用whlie(~scanf("%d", n)) 就超时!!!让我尝试了各个个种方法。

代码:

1.

#include<stdio.h>
#include<string.h>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10005;
int max(int a, int b) {return a > b ? a : b;}
int n, tim;
struct S {
	int v, t;
	friend bool operator < (S a, S b) {
		return a.v < b.v;
	}
} s[N];


void init() {
	tim = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &s[i].v, &s[i].t);
		tim = max(tim, s[i].t);
	}
}

bool cmp(S a, S b) {
	return a.t > b.t;
}
int solve() {
	int ans = 0, j = 0;
	sort(s, s + n, cmp);
	priority_queue<S>Q;
	for (int i = tim; i >= 1; i--) {
		while (s[j].t >= i && j != n) {Q.push(s[j++]);}
		if (!Q.empty()) {
			ans += Q.top().v;
			Q.pop();
		}
	}
	return ans;
}

int main(){
	while (cin>>n) {
		init();
		printf("%d\n", solve());
	}
	return 0;
}

2.

#include<stdio.h>
#include<string.h>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

const int N = 10005;
int max(int a, int b) {return a > b ? a : b;}
int n;
struct S {
	int v, t;
	friend bool operator < (S a, S b) {
		return a.v > b.v;
	}
} s[N];


void init() {
	for (int i = 0; i < n; i++)
		scanf("%d%d", &s[i].v, &s[i].t);
}

bool cmp(S a, S b) {
	return a.t < b.t;
}

int solve() {
	int ans = 0;
	sort(s, s + n, cmp);
	priority_queue<S>Q;
	for (int i = 0; i < n; i++) {
		if (s[i].t > Q.size()) {
			ans += s[i].v;
			Q.push(s[i]);
		}
		else if(s[i].v > Q.top().v) {
			ans -= Q.top().v;
			Q.pop();
			ans += s[i].v;
			Q.push(s[i]);
		}
	}
	return ans;
}

int main(){
	while (cin>>n) {
		init();
		printf("%d\n", solve());
	}
	return 0;
}

3.

#include<stdio.h>
#include<string.h>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10005;

int n, next[N];
struct S {
	int v, t;
} s[N];

void init() {
	memset(next, -1, sizeof(next));
	for (int i = 0; i < n; i++)
		scanf("%d%d", &s[i].v, &s[i].t);
}

bool cmp(S a, S b) {
	return a.v > b.v;
}

int find(int x) {
	return next[x] == -1 ? x : next[x] = find(next[x]);
}

int solve() {
	int ans = 0;
	sort(s, s + n, cmp);
	for (int i = 0; i < n; i++) {
		int t = find(s[i].t);
		if (t) {
			ans += s[i].v;
			next[t] = t - 1;
		}
	}
	return ans;
}

int main(){
	while (cin >> n) {
		init();
		printf("%d\n", solve());
	}
	return 0;
}


UVA 1316 - Supermarket(贪心+经典问题)

上一篇:微信运营必须收藏的软件工具网站合集


下一篇:设计模式10——结构型模式之装饰者模式