-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathmain.cpp
289 lines (232 loc) · 6.27 KB
/
main.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <bits/stdc++.h>
using namespace std; // NOLINT
#define TUTORIAL 0
constexpr int MAX_N = 200000;
#if !TUTORIAL
/**
* ============================================================
* ORIGINAL SOLUTION
* ============================================================
*/
/**
* The number of bids.
*/
int N;
/**
* Each S[i] is an ordered list of (j, k) pairs for person i. In each (j, k)
* pair k is the number of bids person i made on and after the j-th bid. The
* j's in each S[i] should be stored in increasing order.
*/
vector<pair<int, int>> S[MAX_N];
/**
* Stores the person of each bid.
*/
int A[MAX_N];
/**
* Stores the size of each bid.
*/
int B[MAX_N];
/**
* Stores the people to exclude from the bidding sequence during a query.
*/
vector<int> L;
/**
* Counts the number of bids people in L made after the i-th bid in
* O(|L| log N) time.
*/
int after(int i) {
int count = 0;
for (int a : L) {
auto it = upper_bound(S[a].begin(), S[a].end(), make_pair(i, MAX_N + 1));
if (it != S[a].end()) {
count += it->second;
}
}
return count;
}
/**
* Returns the index of the winning bid after removing people in L from the
* auction in O(|L| log N) time. Returns -1 if there is no winning bid -
* happens if L contains all original bidders.
*/
int query() {
int l = 0;
int r = N - 1;
int k = -1; // Index of winning bid.
// Binary search to find the last bid that can be a winner - even if it's
// the same person out-bidding themselves. A winning bid is one that (1) not
// by someone in L (excluded from the auction) and (2) has no other bids
// after it by people not excluded from the auction.
while (l <= r && k == -1) {
int m = l + (r - l) / 2;
// Count the number of binds by people excluded from the auction after m.
int count = after(m);
assert(count <= N - 1 - m);
// The number of original bids after m is N - 1 - m. Use this to check
// if there are any bids after m by someone not excluded from the auction -
// even if that bidder (unknown) is the same person as bidder of bid m.
if (count < N - 1 - m) {
l = m + 1;
} else if (binary_search(L.begin(), L.end(), A[m])) {
// Every bid after and including m is by an excluded person.
r = m - 1;
} else {
k = m;
}
}
if (k != -1) {
// Now we need to handle the case where after removal of all people in L,
// it looks like bidder of bid k was out-bidding himself.
int a = A[k];
assert(k == S[a].rbegin()->first);
int l = 0;
int r = S[a].size() - 1;
// We handle this by binary searching through their bids in O(log N) time.
while (l <= r) {
int m = l + (r - l) / 2;
int i = S[a][m].first;
// Check if there is someone besides bidder a who placed a bid after bid
// i. The number of bids by people not excluded from the auction after
// bid i is (N - 1 - i) - after(i). We check if this value is greater
// than the number of bids placed by bidder a after bid i.
if (after(i) + S[a][m].second - 1 < N - 1 - i) {
// Whoa! Someone excluded from the auction placed a bid after us.
l = m + 1;
} else {
// i is a winning bid, but maybe not the smallest one so keep looking.
k = i;
r = m - 1;
}
}
}
return k;
}
int main() {
int a, b, q, k;
// Parse input.
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a >> b;
a--;
S[a].emplace_back(i, 0);
A[i] = a;
B[i] = b;
}
// Build index S - see declaration at top for info.
for (int a = 0; a < N; a++) {
int count = 0;
for (auto it = S[a].rbegin(); it != S[a].rend(); ++it) {
count++;
it->second = count;
}
}
cin >> q;
for (int i = 0; i < q; i++) {
// Fill L with people to exclude from auction.
cin >> k;
L.resize(k);
for (int j = 0; j < k; j++) {
cin >> L[j];
L[j]--;
}
sort(L.begin(), L.end());
// Query.
int k = query();
if (k != -1) {
// Note: Using 0-indexed bids and people.
cout << (A[k] + 1) << " " << B[k] << endl;
} else {
cout << "0 0" << endl;
}
}
return 0;
}
#else
/**
* ============================================================
* TUTORIAL SOLUTION
* ============================================================
*/
/**
* Stores the maximum bid of each person.
*/
map<int, int> M;
/**
* Stores the bids of each person in increasing order.
*/
vector<int> B[MAX_N];
/**
* Stores the people to exclude from the bidding sequence during a query.
*/
vector<int> L;
/**
* Returns the max bid for person a.
*/
int max_bid(int a) {
assert(!B[a].empty());
return B[a].back();
}
int main() {
int n, a, b, q, k, l;
// Parse input
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
a--;
B[a].push_back(b);
}
// Gather maximum bid for each person.
for (int a = 0; a < n; a++) {
if (!B[a].empty())
M[max_bid(a)] = a;
}
cin >> q;
for (int i = 0; i < q; i++) {
cin >> k;
L.clear();
// Delete each excluded person from the set of max bids.
for (int j = 0; j < k; j++) {
cin >> l;
l--;
if (B[l].empty())
continue;
L.push_back(l);
M.erase(max_bid(l));
}
if (!M.empty()) {
int b = M.rbegin()->first;
int a = M.rbegin()->second;
if (M.size() > 1) {
// See if we can decrease the bid of person a to be just greater than
// the second highest bid.
int l = 0;
int r = B[a].size() - 1;
int c = (++M.rbegin())->first;
while (l <= r) {
int m = l + (r - l) / 2;
if (B[a][m] < c) {
// Bid m of person a is not higher than 2nd highest bid - keep
// looking for a larger bid.
l = m + 1;
} else {
// This is a winning bid - but keep looking for a smaller one.
b = B[a][m];
r = m - 1;
}
}
} else {
// Person a is the only one left - use smallest (initial) bid.
b = B[a][0];
}
cout << (a + 1) << " " << b << endl;
} else {
cout << "0 0" << endl;
}
// Restore set of max bids after query.
for (int a : L) {
M[max_bid(a)] = a;
}
}
return 0;
}
#endif