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.
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. 題目:每個物品有一個價值px,保質期dx,在這保質期之前都可以出售,一天只能出售一個物品,問出售這些物品價值總和最大是多少, 思路:我們可以把物品按照價值排序,我們知道,這題可以貪心,從最大的價值開始判斷可不可以取,我們需要快速查詢該物品保質期之前最近的哪一天,在那天購買該物品,我們可以用并查集來進行查詢操作,我們進行fa[i] = i, rt = Find(i) ① fa[rt] != 0 則說明前面有一天沒有買東西,則我們把fa[rt] = rt - 1,說明我們把那天使用,這樣我們也可以串聯起零散的并查集樹, ② fa[rt] == 0,說明前面所有天都被使用,則無法購買該物品,1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <vector> 6 #include <cmath> 7 8 using namespace std; 9 10 #define ll long long 11 #define pb push_back 12 #define fi first 13 #define se second 14 15 const int N = 10010; 16 struct node 17 { 18 int id, v; 19 20 bool friend operator<(const node& a, const node& b){ 21 return a.v > b.v; 22 } 23 }; 24 int fa[N]; 25 26 int Find(int x){ 27 return fa[x] == x ? x : fa[x] = Find(fa[x]); 28 } 29 30 void solve() 31 { 32 int n; 33 while(~scanf("%d", &n)){ 34 vector<node > vn; 35 36 for(int i = 1; i <= n; ++i){ 37 int id, v; 38 scanf("%d%d", &v, &id); 39 vn.pb({id, v}); 40 } 41 42 sort(vn.begin(), vn.end()); 43 // for(auto vv : vn) cout << vv.v << " "; 44 // cout << endl; 45 for(int i = 0; i < N; ++i) fa[i] = i; 46 47 int profits = 0; 48 for(int i = 0; i < n; ++i){ 49 int rt = Find(vn[i].id); 50 if(rt != 0){ 51 profits += vn[i].v; 52 fa[rt] = rt - 1; 53 } 54 } 55 56 //printf("profits = %d\n", profits); 57 printf("%d\n", profits); 58 } 59 } 60 61 int main() 62 { 63 64 solve(); 65 66 return 0; 67 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/24751.html
標籤:其他
