zoukankan      html  css  js  c++  java
  • [POJ 2976]Dropping tests(0-1分数规划)

    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 .

    Solution

    0-1分数规划裸题

    设能够达到的最大值为p

    则有∑(ai*xi)/∑(bi*xi)<=p

    ∑ai*xi-∑bi*xi*p<=0

    即∑xi(ai-bi*p)<=0

    也就是说∑xi(ai-bi*p)的最大值为0

    二分答案

    如果p<mid ∑xi*(ai-bi*mid)的最大值<0

    如果p>mid ∑xi*(ai-bi*mid)的最大值>0

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define eps 1e-10
    using namespace std;
    int n,k;
    double a[1005],b[1005],c[1005];
    int main()
    {
        while(~scanf("%d %d",&n,&k)&&n)
        {
            for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
            for(int i=1;i<=n;i++)scanf("%lf",&b[i]);
            double l=0,r=1,mid;
            while(r-l>eps)
            {
                mid=(l+r)/2;
                for(int i=1;i<=n;i++)c[i]=a[i]-b[i]*mid;
                sort(c+1,c+1+n);
                double res=0;
                for(int i=k+1;i<=n;i++)
                res+=c[i];
                if(res>0)l=mid;else r=mid;
            }
            printf("%.0f
    ",mid*100);
            
        }
        return 0;
    }
  • 相关阅读:
    石子合并问题(直线版)
    Python_07-常用函数
    Python_06-函数与模块
    C++中的头文件和源文件
    sell 项目 商品表 设计 及 创建
    SpringBoot集成Mybatis
    SpringBoot集成jdbcTemplate/JPA
    SpringBoot使用JSP渲染页面
    SpringBoot引入freemaker前端模板
    使用SpringBoot创建Web项目
  • 原文地址:https://www.cnblogs.com/Zars19/p/6942372.html
Copyright © 2011-2022 走看看