• Dropping tests 二分+01分数规划


    In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be

    .

    Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.

    Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is . However, if you drop the third test, your cumulative average becomes .

    Input

    The input test file will contain multiple test cases, each containing exactly three lines. The first line contains two integers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second line contains n integers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that 0 ≤ ai ≤ bi ≤ 1, 000, 000, 000. The end-of-file is marked by a test case with n = k = 0 and should not be processed.

    Output

    For each test case, write a single line with the highest cumulative average possible after dropping k of the given test scores. The average should be rounded to the nearest integer.

    Sample Input

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

    Sample Output

    83
    100

    Hint

    To avoid ambiguities due to rounding errors, the judge tests have been constructed so that all answers are at least 0.001 away from a decision boundary (i.e., you can assume that the average is never 83.4997).

    01分数规划是这样的一类问题,有一堆物品,每一个物品有一个收益ai,一个代价bi,我们要求一个方案使选择的\sum{ai}/\sum{bi}最大。

    解题思路:

     令r = ∑a[i] * x[i] / (b[i] * x[i])  则必然∑a[i] * x[i] - ∑b[i] * x[i] * r= 0;(条件1)并且任意的 ∑a[i] * x[i] - ∑b[i] * x[i] * max(r) <= 0  (条件2,只有当∑a[i] * x[i] / (b[i] * x[i]) = max(r) 条件2中等号才成立)然后就可以枚举r , 对枚举的r, 求Q(r) = ∑a[i] * x[i] - ∑b[i] * x[i] * r  的最大值,  为什么要求最大值呢?  因为我们之前知道了条件2,所以当我们枚举到r为max(r)的值时,显然对于所有的情况Q(r)都会小于等于0,并且Q(r)的最大值一定是0.而我们求最大值的目的就是寻找Q(r)=0的可能性,这样就满足了条件1,最后就是枚举使得Q(r)恰好等于0时就找到了max(r)。而如果能Q(r)>0 说明该r值是偏小的,并且可能存在Q(r)=0,而Q(r)<0的话,很明显是r值偏大的,因为max(r)都是使Q(r)最大值为0,说明不可能存在Q(r)=0了。
    原文链接:https://blog.csdn.net/dragon60066/article/details/60339056

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    typedef long long ll;
    using namespace std;
    const ll inf = 1e7;
    const int mod = 1000000007;
    const int mx = 1e6; //check the limits, dummy
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(ll i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(ll i=(b),_=(a);i>=_;i--)
    #define clr(a) memset(a, 0, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    int n, t,k;
    double a[mx],b[mx],y[mx];
    int fuck(double x) {
        re(i, 0, n)y[i] = -b[i]*x + a[i];
        sort(y, y + n, greater<double>());
        double sum = 0;
        re(i, 0, n - k)sum += y[i];
        return sum >= 0;
    }
    int main()
    {
        while (~scanf("%d%d", &n, &k), n + k){
            re(i, 0, n)scanf("%lf", &a[i]);
            re(i, 0, n)scanf("%lf", &b[i]);
            double l = 0, r = inf,mid;
            while (r-l>1e-9)
            {
                mid = l+(r-l)/2;
                if (fuck(mid))l = mid;
                else r = mid;
            }
            //cout<<fixed<<setprecision(2)
            printf("%.0lf\n", mid * 100);
        }
        return 0;
    }
  • 相关阅读:
    ubuntu 16.04 tmux
    ubuntu 16.04 samba 文件共享
    ubuntu 16.04 有道词典
    ubuntu bless 16字节每行
    Win7任务栏图标大小调整为等宽
    ubuntu 16.04 vnc server
    ubuntu 16.04 U盘多媒体不自动弹出
    Linux录屏软件
    通过apt-get安装nvidia驱动
    调试X Server
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12671616.html
Copyright © 2020-2023  润新知