# POJ 1456. Supermarket (优先队列/并查集 + 贪心)

## 题面

### Description

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.

## 题解

### 分析

#### 方法 #1：优先队列+贪心

• 如果有 $d_{i} > i$ ，直接将该商品插入堆。
• 如果有 $d_{i} = i$ ，判断该商品利润是否高于队首商品利润（当前堆中最小利润），如果是则弹出队首并将该商品插入堆。

#### 方法 #2：并查集+贪心

• 查询 $d_{i}$所在的树根 $r$ ，并把该商品安排于第$r$ 天售出。
• 合并 $r$到$r-1$ 。

### 代码

#### 方法 #1

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120  // #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef long double ld; const int INF = 0x3f3f3f3f; const double PI = acos(-1.0); // #define ffor(_var, _begin, _end, ...) \ // for(decay::type _var = _begin; _var < _end; __VA_ARGS__) #define ffor(_var, _begin, _end, ...) \ for(int _var = _begin; _var < _end; __VA_ARGS__) // #define rfor(_var, _rbegin, _rend, ...) \ // for(decay::type _var = _rbegin; _var > _rend; __VA_ARGS__) #define ABS(x) ((x) > 0 ? (x) : -(x)) // #if defined _WIN32 // // #define _WINDEBUG // #include // inline std::ostream& yellow(std::ostream &s) { // HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); // SetConsoleTextAttribute(hStdout, // FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY); // return s; // } // inline std::ostream& white(std::ostream &s) { // HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); // SetConsoleTextAttribute(hStdout, // FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); // return s; // } // #elif defined __linux__ // // #define _LINUXDEBUG // #define yellow "\033[33;1m" // #define white "\033[0m" // #endif #if defined _WINDEBUG || defined _LINUXDEBUG #define debug(x...) \ do { cout << yellow << #x << " -> "; err(x); } while(0) void err() { cout << white << endl; } template void err(T a, A... x) { cout << a << ' '; err(x...); } template class T, typename t, typename... A> void err(T a, A... x) { for(auto& v : a) cout << v << ' '; err(x...); } #else #define debug(...) #endif /*********************************************************************/ const int MAXN = 10010; struct goods { int p, d; bool operator < (const goods& g) const { return (d == g.d ? p < g.p : d < g.d); } bool operator > (const goods& g) const { return (p == g.p ? d > g.d : p > g.p); } friend ostream& operator << (ostream& co, const goods& g); }; ostream& operator << (ostream& co, const goods& g) { co << "(" << g.p << ", " << g.d << ") "; return co; } goods a[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("test.in", "r", stdin); int n = 0; while(cin >> n) { ffor(i, 1, n + 1, i++) { cin >> a[i].p >> a[i].d; } sort(a + 1, a + n + 1); debug(vector(a + 1, a + n + 1)); priority_queue, greater > q; ffor(i, 1, n + 1, i++) { if(a[i].d > (int)q.size()) { q.push(a[i]); } else if(a[i].d == (int)q.size()) { if(a[i].p > q.top().p) { q.pop(); q.push(a[i]); } } else { while(true); } } vector v; ll ans = 0; while(!q.empty()) { v.push_back(q.top()); ans += q.top().p; q.pop(); } debug(v); cout << ans << endl; } return 0; } 

#### 方法 #2

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144  /*************************************************************/ #if __cplusplus < 201103L // For jury which unsupports C++11 #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ffor(_var, _begin, _end, ...) \ for(__typeof__(_end) _var = _begin; _var < _end; __VA_ARGS__) #define rfor(_var, _rbegin, _rend, ...) \ for(__typeof__(_rbegin) _var = _rbegin; _var > _rend; __VA_ARGS__) #define cfor(_var, _cbegin, _cend, ...) \ for(__typeof__(_cend) _var = _cbegin; _var != _cend; __VA_ARGS__) #else #include using namespace std; #define ffor(_var, _begin, _end, ...) \ for(decay::type _var = _begin; _var < _end; __VA_ARGS__) #define rfor(_var, _rbegin, _rend, ...) \ for(decay::type _var = _rbegin; _var > _rend; __VA_ARGS__) #endif typedef long long ll; typedef long double ld; #define ABS(x) ((x) > 0 ? (x) : -(x)) // #if defined _WIN32 // #define _WINDEBUG // #include // inline std::ostream& yellow(std::ostream &s) { // HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); // SetConsoleTextAttribute(hStdout, // FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY); // return s; // } // inline std::ostream& white(std::ostream &s) { // HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); // SetConsoleTextAttribute(hStdout, // FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); // return s; // } // #elif defined __linux__ // #define _LINUXDEBUG // #define yellow "\033[33;1m" // #define white "\033[0m" // #endif #if defined _WINDEBUG || defined _LINUXDEBUG #define ar2vec(_begin, _end) \ vector::value_type>::type>(_begin, _end) #define debug(x...) \ do { cout << yellow << #x << " -> "; err(x); } while(0) void err() { cout << white << endl; } template void err(T a, A... x) { cout << a << ' '; err(x...); } template class T, typename t, typename... A> void err(T a, A... x) { for(auto& v : a) cout << v << ' '; err(x...); } #else #define ar2vec(...) #define debug(...) #endif /*************************************************************/ const int MAXN = 10010; struct dsu { int fa[MAXN]; void init(int n) { ffor(i, 1, n + 1, i++) fa[i] = i; } int get(int x) { return (x == fa[x] ? x : (fa[x] = get(fa[x]))); } void merge(int x, int y) { fa[get(x)] = get(y); } }; struct goods { int p, d; bool operator < (const goods& g) const { return (p == g.p ? d < g.d : p < g.p); } friend ostream& operator << (ostream& co, const goods& g); }; ostream& operator << (ostream& co, const goods& g) { co << "(" << g.p << ", " << g.d << ") "; return co; } dsu d; goods a[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("test.in", "r", stdin); int n = 0; while(cin >> n) { ffor(i, 1, n + 1, i++) cin >> a[i].p >> a[i].d; sort(a + 1, a + n + 1); debug(ar2vec(a + 1, a + n + 1)); map m; vector v; d.init(10005); rfor(i, n, 0, i--) { int id = d.get(a[i].d); debug(i, a[i], id); if(m.find(id) == m.end() && id >= 1) { debug("test", i, a[i], id); m.insert(make_pair(id, true)); d.merge(id, id - 1); v.push_back(a[i]); } } debug(v); ll ans = 0; int sz = (int)v.size(); ffor(i, 0, sz, i++) ans += v[i].p; cout << ans << endl; } return 0; }