https://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html
参考这个博客
#include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #define _for(i,a,b) for(int i = a;i<b; i++) #define N 100005 #define eps 1e-7 using namespace std; int n,k; struct Node{ int v,id, w; double d; bool operator < (const Node o)const{ return d>o.d; } }a[N]; double check(double num) { for(int i=0;i<n;i++) { a[i].d = a[i].v - num*a[i].w; } sort(a,a+n); double sum=0.0; for(int i=0;i<k;i++) { sum+=a[i].d; } return sum>0; } int main() { scanf("%d%d",&n,&k); double l=0.0,r=1.0,mid; for(int i=0;i<n;i++) { scanf("%d %d",&a[i].v,&a[i].w); a[i].id = i+1; r = max(r,a[i].v*1.0/a[i].w); } while(r-l>eps) { mid=(l+r)/2; if( check(mid) )l=mid; else r=mid; } vector<int> V; _for(i,0,k){ V.push_back(a[i].id); } sort( V.begin(),V.end() ); _for(i,0,k) cout<<V[i]<<" "; cout<<endl; return 0; }