51nod1105(二分)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105
题意:中文题诶~
思路:直接二分答案,再通过二分找有多少组合大于等于当前值,确定答案;
代码:
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <math.h> 5 #define ll long long 6 using namespace std; 7 8 const int MAXN=1e5; 9 ll a[MAXN], b[MAXN]; 10 11 ll is_ok(ll mid, ll n){ 12 ll ans=0; 13 for(int i=0; i<n; i++){ 14 // ll cnt=ceil(mid*1.0/a[i]);//***这里用ceil会wa,坑死,可能是double转long long有误差 15 ll cnt=mid/a[i]; 16 if(mid%a[i]) cnt+=1; 17 int pos=lower_bound(b, b+n, cnt)-b; 18 ans+=n-pos; 19 } 20 return ans; 21 } 22 23 int main(void){ 24 ll n, k; 25 scanf("%lld%lld", &n, &k); 26 for(int i=0; i<n; i++){ 27 scanf("%lld%lld", &a[i], &b[i]); 28 } 29 sort(a, a+n); 30 sort(b, b+n); 31 ll l=a[0]*b[0], r=a[n-1]*b[n-1]; 32 while(l<=r){ 33 ll mid=(l+r)>>1; 34 ll cnt=is_ok(mid, n); 35 if(cnt>=k) l=mid+1; 36 else r=mid-1; 37 } 38 cout << l-1 << endl; 39 return 0; 40 }
我就是我,颜色不一样的烟火 --- geloutingyu