码迷,mamicode.com
首页 > 其他好文 > 详细

『POJ 2976』Dropping tests (01分数规划)

时间:2018-09-15 13:13:03      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:stream   选择   org   cst   reg   二分答案   规划   com   poj   

题目链接

Descrip

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 技术分享图片.



题目描述

现在有n个物品,每个物品都有两个价值,一个价值为\(a\),一个价值为\(b\),(保证\(a_i\leq b_i\))现在最多可以有k个物品不选,请问能够使选择的物品的\(100*\frac{\sum a_i}{\sum b_i}\)最大是多少?



解题思路

显然是一道01分数规划的题目,因为题目中说明了\(a_i\leq b_i\),那最终的取值就只能是\(0\)\(100\)之间,所以我们把那个100提出来,答案的区间就变成了\([0,1]\),所以,我们可以二分答案去验证可行性。

数学证明

假设\(\frac{\sum a_i}{\sum b_i}\ge x\),那就有\(\sum a_i\ge x*\sum b_i\),即\(\sum a_i-x*\sum b_i\ge0\),我们只要处理出每个物品\(i\)的另一个价值\(a_i-x*b_i\),然后从大到小排序,计算前\(n-k\)个物品的价值的和,是否大于等于0就好了。

一点小细节

注意一下eps的设置就好了。



代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1050;
const double eps=1e-5;
int n,k;
int a[maxn],b[maxn];
double tmp[maxn];
inline bool cmp(double x,double y){
    return x>y;
}
inline bool check(double x){
    for(register int i=1;i<=n;i++){
        tmp[i]=(double)a[i]-1.0*b[i]*x;
    }
    sort(tmp+1,tmp+1+n,cmp);
    double ans=0;
    for(register int i=1;i<=n-k;i++)ans+=tmp[i];
    return ans>=0;
}
int main(){
    while(~scanf("%d%d",&n,&k)){
        if(n+k==0)return 0;
        for(register int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(register int i=1;i<=n;i++)scanf("%d",&b[i]);
        register double l=0.0,r=1.0;
        while(r-l>eps){
            register double m=(l+r)/2;
            if(check(m))l=m;
            else r=m;
        }
        l*=100;
        printf("%.0lf\n",l);
    }
}

『POJ 2976』Dropping tests (01分数规划)

标签:stream   选择   org   cst   reg   二分答案   规划   com   poj   

原文地址:https://www.cnblogs.com/Fang-Hao/p/9650615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!