http://poj.org/problem?id=3111
这题需要输出任意一个方案,我加了一个结构体排序,水题。。不过之前精度取得有点高,TLE了,但是取得太小会WA,我试过
/*AC*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
int dir;
double f;
};
const double eps = 1e-4; //改成1e-5就超时,1e-3就WA
const int inf = 1e9 + 7;
double a[100000+5], b[100000+5];
node c[100000+5];
int n, k;
bool cmp(node i, node j){
return i.f > j.f;
}
/*
bool cmp2(node i, node j){
return i.dir < j.dir;
}
*/
bool C(double v){
for(int i=0; i<n; i++){
c[i].dir = i;
c[i].f = a[i] - v*b[i];
}
sort(c, c+n, cmp);
double sum = 0;
for(int i=0; i<k; i++)
sum += c[i].f;
if(sum >= 0) return 1;
else return 0;
}
int main(){
while(scanf("%d %d", &n, &k) == 2){
double lb = inf, ub = 0;
for(int i=0; i<n; i++){
scanf("%lf %lf", &a[i], &b[i]);
if(a[i]/b[i] < lb) lb = a[i]/b[i];
if(a[i]/b[i] > ub) ub = a[i]/b[i];
}
ub++;
while(ub - lb > eps){
double mid = (lb+ub)/2;
if(C(mid)) lb = mid;
else ub = mid;
}
//sort(c, c+k, cmp2); //要不要这个排序都无所谓,我试过,因为是special judge
for(int i=0; i<k; i++)
printf("%d ", c[i].dir+1);
printf("\n");
}
return 0;
}