BZOJ 5163.「BJWC2018」第 k 大斜率

显然先考虑二分答案。
于是如何判定?

即,求斜率大于 midmid 的直线条数。
xi<xjx_i < x_j,如果 iijj 确定的直线的斜率大于 kk,根据定义有

于是问题变成了统计 xi<xj,yikxi<yjkxjx_i < x_j,y_i - kx_i < y_j - kx_j 的二元组 (i,j)(i,j) 的个数。
就是一个二维偏序,离散化之后树状数组解决。

代码:

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
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) ((x) & -(x))
using namespace std;
const int N = 1e5;
int n;
long long k;
struct note
{
int x,y,id;
long long v;
inline bool operator<(const note &o) const
{
return v < o.v || (v == o.v && x < o.x);
}
} a[N + 5];
int ind[N + 5],len;
int c[N + 5];
inline void update(int x)
{
for(;x <= len;x += lowbit(x))
++c[x];
}
inline int query(int x)
{
int ret = 0;
for(;x;x -= lowbit(x))
ret += c[x];
return ret;
}
int l,r,mid,ans;
int check()
{
memset(c,0,sizeof c);
for(register int i = 1;i <= n;++i)
a[i].v = a[i].y - (long long)mid * a[i].x;
sort(a + 1,a + n + 1);
long long cnt = 0;
for(register int i = 1;i <= n;update(a[i++].id))
cnt += query(a[i].id - 1);
return cnt >= k;
}
int main()
{
scanf("%d%lld",&n,&k);
for(register int i = 1;i <= n;++i)
scanf("%d%d",&a[i].x,&a[i].y),ind[i] = a[i].x;
sort(ind + 1,ind + n + 1),len = unique(ind + 1,ind + n + 1) - ind - 1;
for(register int i = 1;i <= n;++i)
a[i].id = lower_bound(ind + 1,ind + len + 1,a[i].x) - ind;
l = -2e8,r = 2e8;
while(l <= r)
{
mid = l + r >> 1;
check() ? (ans = mid,l = mid + 1) : r = mid - 1;
}
printf("%d\n",ans);
}