其实还是01分数规划,因为要输出分数形式,所以要用结构体存数据,一起排序,每次记录分子分母,最后在输出就好啦
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1e5+5;
int k,n;
const double eps = 1e-6;
int ta,tb;
struct node{
int a,b;
double c;
bool operator < (const node &t)const{
return c>t.c;
}
}p[maxn];
bool check(double mid)
{
double ans = 0; ta=tb = 0;
for(int i = 0; i<n; i++)
p[i].c = p[i].a - mid*p[i].b;
sort(p,p+n);
for(int i = 0; i<k; i++)
ans += p[i].c,ta+=p[i].a,tb+=p[i].b;
return ans>=0;
}
void solve ()
{
double l = 0,r = 1000000000;
while(r-l>eps)
{
double mid = (l+r)/2;
if(check(mid)) l = mid;
else r = mid;
}
//printf("%lf\n",l);
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&k);
for(int i = 0; i<n; i++)
{
scanf("%d%d",&p[i].b,&p[i].a);
}
solve();
int t = __gcd(ta,tb);
printf("%d/%d",ta/t,tb/t);
return 0;
}