用了二分套三分。
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
int n,k;
struct node{
int x,y;
}a[N];
int cal(int type,int x,int t){
int ans = 0;
for(int i = 1;i <= n;i++){
if((type ? a[i].x : a[i].y) <= x)ans += (x - (type ? a[i].x : a[i].y));
else if((type ? a[i].x : a[i].y) >= x + t)ans += ((type ? a[i].x : a[i].y) - x - t);
}
return ans;
}
int calc(int type,int t){
int l = 0,r = INT_MAX - t;
while(r - l > 2){
int mid1 = l + (r - l) / 3,mid2 = r - (r - l) / 3;
if(cal(type,mid1,t) <= cal(type,mid2,t))r = mid2;
else l = mid1;
}
int ans = INT_MAX;
for(int i = l;i <= r;i++)ans = min(ans,cal(type,i,t));
return ans;
}
bool check(int t){
int cnt = 0;
cnt += calc(1,t);
cnt += calc(0,t);
return cnt <= k;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1;i <= n;i++)cin >> a[i].x >> a[i].y;
int l = -1,r = INT_MAX;
while(r - l > 1){
int mid = (l + r) / 2;
if(check(mid))r = mid;
else l = mid;
}
cout << r;
}
WA + TLE。