POJ 2976 Dropping tests 01分数规划

来源:互联网 发布:t型截面惯性矩的算法 编辑:程序博客网 时间:2024/05/22 09:09

先从ZP那里粘了点东西过来,再加入自己的理解。

摘:

题目大意就 给定n个二元组(a,b),扔掉k个二元组,使得剩下的a元素之和与b元素之和的比率最大

     题目求的是 max(∑a[i] * x[i] / (b[i] * x[i]))  其中a,b都是一一对应的。 x[i]取0,1  并且 ∑x[i] = n - k;

    转:那么可以转化一下。  令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了。

自习感受:

我用简单的方式在描述一下:a代表a[i]中被选择的数字的和,同理b.这样就会有一个公式a/b = r<==>a-b*r == 0, 这里的r代表的是最大的结果。然后看看此时当r确定时,最优的结果r1与r进行比较,如果a-b*r1==0,说明枚举到此时的r就是最优的解,如果r1>r(a-b*r1 > 0(这里的r1就是枚举出来的结果)),则枚举结果偏大,相反偏小。注意将公式变形可以得到:a-b*r == 0.在这里ai,和bi的选择是同时的,如果a中没有ai,则一定没有bi所以就建立起来联系了啊。这里剔除任意一个数字都是可以的,所以每个数字对应两个状态0剔除,1不剔除。然后我们就设c==a-b*r1,(r1是枚举出来的结果)因为我们想是得r最大,所以我们就在数组c[i] = a[i]-b[i]*r1,中选择最大的n-m个数字,然后就算出一个数字来,根据前面说的进行判断,是否符合要求。

感觉说的有点乱,有点啰嗦了啊,总之就是先建立起来a,b与r的关系。然后比较求出来的r1与要求的r的大小,来二分求r。

Dropping tests
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5347 Accepted: 1851

Description

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 nintegers 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 15 0 25 1 64 21 2 7 95 6 7 90 0

Sample Output

83100
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 1000100//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 1010;using namespace std;int a[maxn], b[maxn];double c[maxn];int main(){    int n, m;    while(cin >>n>>m)    {        if(!n && !m)            break;        for(int i = 0; i < n; i++)            cin >>a[i];        for(int i = 0; i < n; i++)            cin >>b[i];        double l = 0.0;//0%;        double r = 1.0;//100%;        double mid = (l+r)/2;        while(abs(r-l) > eps)        {            mid = (l+r)/2;            for(int i = 0; i < n; i++)                c[i] = 1.0*a[i]-1.0*b[i]*mid;//算出来所有的ci来            double sum = 0;            sort(c, c+n);            for(int i = m; i < n; i++)                sum += c[i];//求出和来            if(sum < 0)//这说明枚举的r1偏小                r = mid;            else                l = mid;        }        mid *= 100;        cout<<fixed<<setprecision(0)<<mid<<endl;    }    return 0;}


0 0
原创粉丝点击