Dropping tests POJ - 2976 ——01分数规划+二分 ——很奇妙的一个题 各个击破

博客目录

一、引言

    好久之前做过这道题,而且是提交了好多次AC代码,前几天又遇到类似的依然懵逼,不光我懵逼,一块做题的acmer也跟着懵逼了,而且大家都做过.......好了被钉在耻辱柱子上了。所以今天又回来补一下博客。

二、原题

vjudge原题传送门

In a certain course, you take n tests. If you get ai out of biquestions 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 kof 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).

三、思路

大意:给n个数对(ai,bi),现在删掉不超过k个数对,使得最大。

     看上去这道题有点像溶液浓度问题——贪心。但是你用公式化简溶液加入两种新的溶液的密度条件:(a+c)/(b+d) <(a+e)/(b+f)

化简结果为:af+c(b+f)<ad+e(b+d),跟a和b有关(也就是跟以前溶液的情况有关),现在的决策受到以前决策的影响,无法贪心。

     正解为:

设二分变量为ans,每次check  原式能否达到ans, 原式>=ans,也就是

ansa<=100*sigma(ai)/sigma(bi),去分母,然后移项:

ans*sigma(bi)-sigma(ai)<=0;

观察公式,形式为一次函数形式:f(ans)=a*ans+b <= 0的形式,所以可以用对ans二分求解。

而check函数:

将sigma合并

sigma(ans*bi-ai)<=0;

然后对ans*bi-ai排序,有序之后上述公式求和过程便是单调的了,我们check验证此时原式能否满足原式>=ans,所以我们很容易从大到小检查前n-k个求和是否大于等于0,大于等于0就是可以满足条件原式>=ans,所以ans小了应该往上偏移,反之亦然。

时间复杂度:二分ans:log100<7  乘以 [   check时排序了一下nlogn  加上  并列的从大到小遍历n]    结果为O(7nlogn)

四、AC代码

//#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
/*
二分
https://vjudge.net/problem/POJ-2976
取前n-k大的 
*/
typedef long long ll;
typedef struct
{
    ll x,y;
}Tone;
Tone co[1010];
ll n,k;
ll s;
double c[1010];
int check(double ans)
{
    int i;
    int p=n-1;
    double sum=0;

    for(i=0;i<n;i++)
        c[i]=co[i].x*100-ans*co[i].y;
    sort(c,c+n);
    for(i=0;i<s;i++)
    {
        sum+=c[p];
        p--;
    }
    return sum>=0;
}
int main()
{
    double m;
    ll i,k;
    double a,b;
    while(cin>>n>>k,n||k)
    {
        s=n-k;
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&co[i].x);
        }
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&co[i].y);
        }
        a=0;
        b=100;
        int x=200;//二分200次精度就差不多够了 
        while(x--)
        {
            m=(a+b)/2;
            if(check(m))
            {
                a=m;
            }
            else b=m;
        }
        printf("%.0f\n",a);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值