We shall meet in place where there is no darkness.

0%

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

1
2
3
4
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

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

题目大意

商店有 nn件商品 (0n10000)(0 \leq n \leq 10000),每件商品各有其利润 pip_{i}和过期时间did_{i}(1pi,di10000)(1 \leq p_{i}, d_{i} \leq 10000),现商店每天只欲出售一件商品,且过期商品不能再出售,求问商店可获得的最大利润。

题解

分析

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

由于对第 tt(ti=1ndi)( \displaystyle t \in \bigcup\limits_{i=1}^{n}d_{i})可售出的商品最多有 tt件,因此在第tt 天显然总希望售出允许范围内利润前tt 大的商品;因此我们动态维护利润前tt 大的方案表即可。

先将商品按过期时间从小到大排序,之后新建一个空的小根堆(权值为商品利润),之后遍历每件商品,对第 ii 件商品判断:

  • 如果有 di>id_{i} > i ,直接将该商品插入堆。
  • 如果有 di=id_{i} = i ,判断该商品利润是否高于队首商品利润(当前堆中最小利润),如果是则弹出队首并将该商品插入堆。

之后统计堆中所有商品利润总和即可。

方法 #2:并查集+贪心

还可以这样考虑:我们在优先卖出利润大的商品的同时,还希望每件商品卖得尽量晚。因此,我们需要维护当前已经有商品出售的日期,根据 did_{i}天之前日期的占用情况来确定第ii 件商品的出售日期。

将商品按利润从大到小排序,并建立一个关于日期的并查集。遍历所有商品,对第 ii 件商品进行如下操作:

  • 查询 did_{i}所在的树根 rr ,并把该商品安排于第rr 天售出。
  • 合并 rrr1r-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 <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
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<decltype(_end)>::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<decltype(_rbegin)>::type _var = _rbegin; _var > _rend; __VA_ARGS__)
#define ABS(x) ((x) > 0 ? (x) : -(x))

// #if defined _WIN32
// // #define _WINDEBUG
// #include <windows.h>
// 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<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
template<template<typename...> class T, typename t, typename... A>
void err(T<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<goods>(a + 1, a + n + 1));

priority_queue<goods, vector<goods>, greater<goods> > 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<goods> 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 <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
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 <bits/stdc++.h>
using namespace std;

#define ffor(_var, _begin, _end, ...) \
for(decay<decltype(_end)>::type _var = _begin; _var < _end; __VA_ARGS__)
#define rfor(_var, _rbegin, _rend, ...) \
for(decay<decltype(_rbegin)>::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 <windows.h>
// 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<decay<iterator_traits<decltype(_begin)>::value_type>::type>(_begin, _end)
#define debug(x...) \
do { cout << yellow << #x << " -> "; err(x); } while(0)
void err() { cout << white << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
template<template<typename...> class T, typename t, typename... A>
void err(T<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<int, bool> m;
vector<goods> 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;
}
  • 本文作者: Panelatta
  • 本文链接: https://bofc.tech/55734929/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!