题解:P4479 [BJWC2018] 第 k 大斜率
\texttt{solve}
小清新题,就是细节处理有点恶心。
二分一下斜率
把点按
把
此时先
当然,预处理
\texttt{code}
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 1e5+5;
const int INF = 2e8;
struct node{
int x,y;
}arr[N];
int n,t[N];
lint k,a[N],b[N];
inline bool cmp(node x,node y){
if(x.x != y.x)
return x.x < y.x;
return x.y > y.y;
}
inline int lowbit(int x){
return x&(-x);
}
inline void updateAdd(int x,int v){
while(x <= n){
t[x] += v;
x += lowbit(x);
}
}
inline int query(int x){
int res = 0;
while(x > 0){
res += t[x];
x -= lowbit(x);
}
return res;
}
inline lint check(int mid){
memset(t,0,sizeof(t));
for(int i = 1; i <= n; i++)
b[i] = a[i] = arr[i].y - 1ll*mid*arr[i].x;
sort(b+1,b+1+n);
int m = unique(b+1,b+n+1) - b - 1;
for(int i = 1; i <= n; i++)
a[i] = lower_bound(b+1,b+m+1,a[i]) - b;
lint res = 0;
for(int i = 1; i <= n; i++){
res += query(a[i]);
updateAdd(a[i],1);
}
return res;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> arr[i].x >> arr[i].y;
sort(arr+1,arr+1+n,cmp);
int l = -INF,r = INF,ans;
while(l <= r){
int mid = l + r >> 1;
lint res = check(mid);
if(res < k) r = mid - 1;
else{
ans = mid;
l = mid + 1;
}
}
cout << ans << endl;
return 0;
}
\texttt{tips}
- 对点排序时,
x 相等时对y 进行降序排序; - 注意处理一些东西的时候要开
\texttt{long long} ; - 二分的边界是
\pm 2\times10^8 ; - 每次二分答案前清空树状数组/线段树。
对于第一条
值得一提的是当时在评论区看到了一个错误言论说是为了避免计算