# 传送门

https://www.luogu.org/problemnew/show/P4479

# ac代码

#include <bits/stdc++.h>
#define ll long long
#define db double
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
template <typename T>
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
#define N 100005
struct data {
ll x, y, id, val;
bool operator <(const data &rhs) const {
return val == rhs.val ? x < rhs.x : val < rhs.val;
}
}a[N];
struct BIT {
#define lowbit(x) (x&-x)
ll tr[N];
int n;
void init(int nn) {
ms(tr, 0);
n = nn;
}
void add(int x, int val) {
for (; x <= n; x += lowbit(x)) tr[x] += val;
}
ll query(int x) {
ll res = 0;
for (; x; x -= lowbit(x)) res += tr[x];
return res;
}
}tr;
int n;
int disc_x[N];
ll k;
bool check(int x) {
for (int i = 1; i <= n; i ++) {
a[i].val = a[i].y - 1ll * x * a[i].x;
}
sort(a + 1, a + 1 + n);
tr.init(n);
ll ans = 0;
for (int i = 1; i <= n; i ++) {
ans += tr.query(a[i].id - 1);
}
return ans >= k;
}
int main() {
for (int i = 1; i <= n; i ++) {
disc_x[i] = a[i].x;
}
sort(disc_x + 1, disc_x + 1 + n);
int disc_tot = unique(disc_x + 1, disc_x + 1 + n) - disc_x - 1;
for (int i = 1; i <= n; i ++) {
a[i].id = lower_bound(disc_x + 1, disc_x + 1 + disc_tot, a[i].x) - disc_x;
}
int l = -inf, r = inf, ans;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) l = mid + 1, ans = mid;
else r = mid - 1;
}
printf("%d\n", ans);
return 0;
}

[luogu4479][BJWC2018]第k大斜率【二维偏序+二分+离散化+树状数组】

(0)
(0)

0条