POJ ~ 2976 ~ Dropping tests (01分数规划)

题意

给你N个物品,每个物品有一个价值Vaule和花费Cost,让你挑选N - K个,使得\frac{\sum vaule}{\sum cost}最大。

 

题解

大佬博客链接:01分数规划问题相关算法与题目讲解(二分法与Dinkelbach算法)

这个题实在序列上的01分数规划,直接按vaule - x*cost排序就好了。

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 5;
const double eps = 1e-5;

int n, k, a[MAXN], b[MAXN];
double d[MAXN];

bool check(double x)
{
    for (int i = 0; i < n; i++) d[i] = a[i] - x*b[i];
    sort(d, d+n);
    double ans = 0;
    for (int i = n-1; i >= k; i--) ans += d[i];
    return ans >= 0;
}

int main()
{
    while(~scanf("%d%d", &n, &k) && (n+k))
    {
        for (int i = 0; i < n; i++) scanf("%d", &a[i]);
        for (int i = 0; i < n; i++) scanf("%d", &b[i]);
        double l = 0, r = 1;
        while (r-l > eps)
        {
            double mid = (l+r)/2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        printf("%.0f\n", l*100);
    }
    return 0;
}

/*
3 1
5 0 2
5 1 6
4 2
1 2 7 9
5 6 7 9
0 0
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值