poj 2976 Dropping tests 01分数规划

题目链接:http://poj.org/problem?id=2976

01分数规划的问题 需要适当变形 然后贪心做

我是照着这个博客学的:http://blog.csdn.net/hhaile/article/details/8883652

这题另一个教训是

比如说测试样例以“0 0”结束

那应该这么写while(scanf("%d%d", &n, &k) == 2 && !(n == 0 && k == 0))

而不该这样while(scanf("%d%d", &n, &k) == 2 && n != 0 && k != 0)

强制类型转换不会四舍五入 只是截断

所以输出那样写可以达到四舍五入的目的

注意一下二分的写法

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 1010;
const double eps = 1e-7;

double a[maxn], b[maxn];
double c[maxn];


int main()
{
    //freopen("in.txt", "r", stdin);

    int n, k;
    while(scanf("%d%d", &n, &k) == 2 && !(n == 0 && k == 0))
    {
        for(int i = 0; i < n; i++)
            scanf("%lf", &a[i]);
        for(int i = 0; i < n; i++)
            scanf("%lf", &b[i]);

        double lb = 0, ub = 1;

        while(ub - lb > eps)
        {
            double r = (lb + ub) / 2;

            for(int j = 0; j < n; j++)
                c[j] = a[j] - r * b[j];

            sort(c, c + n);

            double sum = 0;
            for(int j = n-1; j >= k; j--)
                sum += c[j];

            if(sum > 0)
                lb = r;
            else
                ub = r;

        }

        printf("%d
", (int) (100 * ub + 0.5));

    }


    return 0;
}
原文地址:https://www.cnblogs.com/dishu/p/4302266.html