forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
139 lines (114 loc) · 2.82 KB
/
D.cpp
File metadata and controls
139 lines (114 loc) · 2.82 KB
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
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, a) for (int i = 0, _a = (a); i < _a; ++i)
#define DEBUG(X) \
{ cerr << #X << " = " << (X) << endl; }
#define PR(A, n) \
{ \
cerr << #A << " = "; \
FOR(_, 1, n) cerr << A[_] << ' '; \
cerr << endl; \
}
#define PR0(A, n) \
{ \
cerr << #A << " = "; \
REP(_, n) cerr << A[_] << ' '; \
cerr << endl; \
}
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int)(x).size())
using namespace std;
double safe_sqrt(double x) { return sqrt(max((double)0.0, x)); }
int GI(ll& x) { return scanf("%lld", &x); }
const int MN = 300111;
const int INF = 1000111000111000111LL;
int n;
struct Team {
int t, w;
void read() {
GI(t);
GI(w);
}
int toKill() { return w - t + 1; }
} a1, a[MN];
bool operator<(const Team& a, const Team& b) { return a.t > b.t; }
int c[MN];
#define _(X) ((X) & (-(X)))
int sum[MN], sl[MN];
int getSum(int u) {
int res = 0;
for (; u > 0; u -= _(u)) {
res += sum[u];
if (res > INF) res = INF;
}
return res;
}
int getSl(int u) {
int res = 0;
for (; u > 0; u -= _(u)) res += sl[u];
return res;
}
void updateSum(int u, int val) {
for (; u <= n; u += _(u)) {
sum[u] += val;
if (sum[u] > INF) sum[u] = INF;
}
}
void updateSl(int u, int val) {
for (; u <= n; u += _(u)) sl[u] += val;
}
int id[MN];
pair<int, int> t[MN];
void init() {
FOR(i, 1, n) t[i] = make_pair(a[i].toKill(), i);
sort(t + 1, t + n + 1);
FOR(i, 1, n) id[t[i].second] = i;
}
void update(int u) {
updateSum(id[u], t[id[u]].first);
updateSl(id[u], 1);
}
#undef int
int main() {
#define int long long
while (GI(n) == 1) {
memset(sum, 0, sizeof sum);
memset(sl, 0, sizeof sl);
--n;
a1.read();
FOR(i, 1, n) a[i].read();
sort(a + 1, a + n + 1);
init();
FOR(i, 1, n) c[i] = a[i].t;
int nc = unique(c + 1, c + n + 1) - c - 1;
c[++nc] = 0;
int X = 0;
int better = 0;
int res = n + 1;
FOR(i, 1, nc) if (a1.t >= c[i]) {
// reduce a1.t to c[i]
X = a1.t - c[i];
// how many better team?
while (better < n && a[better + 1].t > c[i]) {
++better;
update(better);
}
// can we kill any team?
int l = 1, r = n, killed = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (getSum(mid) <= X) {
killed = mid;
l = mid + 1;
} else
r = mid - 1;
}
res = min(res, better - getSl(killed) + 1);
}
cout << res << endl;
}
}