题目大意:有n颗珍珠,每颗珍珠有相应的价值和代价,要从中挑出K颗珍珠,使得(这k颗珍珠的总价值/这k颗珍珠的代价 )达到最大
解题思路:平均值最大,要注意精度
给出01分数规划的详细解读
< http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100010
int n, k, ans[maxn];
double Sum;
struct jewels {
double v, w, d;
int id;
}J[maxn];
bool cmp(const jewels a, const jewels b) {
return a.d > b.d;
}
bool judge(double mid) {
for(int i = 0; i < n; i++)
J[i].d = J[i].v - mid * J[i].w;
sort(J, J + n, cmp);
double t = 0.0;
for(int i = 0; i < k; i++)
t += J[i].d;
return t >= 0;
}
void solve() {
double l = 0, r = 1e7 + 1;
while(r - l > 1e-6) {
double mid = (l + r) / 2;
if(judge(mid))
l = mid;
else
r = mid;
}
}
int main() {
while(scanf("%d%d", &n, &k) != EOF) {
for(int i = 0; i < n; i++) {
scanf("%lf%lf", &J[i].v, &J[i].w);
J[i].id = i + 1;
}
solve();
printf("%d", J[0].id);
for(int i = 1; i < k; i++)
printf(" %d", J[i].id);
printf("\n");
}
return 0;
}