https://www.nowcoder.com/acm/contest/93/I
先说
这题
没想出来
应该说是一道比较简单的题
想到过要二分答案
没想到要这么二分:
AC代码:
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false)
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const long double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+20;
int n,k;
ld v[maxn],w[maxn],y[maxn];
bool cmp(ld a,ld b){
return a > b;
}
bool check(ld x){
for(int i=0;i<n;i++){
y[i] = v[i]-w[i]*x;
}
sort(y,y+n,cmp);
ld res = 0;
for(int i=0;i<k;i++){
res += y[i];
}
return res >= 0;
}
int main(){
int T;
cin >> T;
while(T--){
cin >> n >> k;
for(int i=0;i<n;i++){
cin >> w[i] >> v[i];
}
ld l = 0,r = INF,mid;
while(fabs(l-r)>eps){
mid = (l+r)/2;
if(check(mid)) l = mid;
else r = mid;
}
cout << fixed << setprecision(2) << l << endl;
}
return 0;
}