poj-2976 Dropping tests(01分数规划)

题目链接:

Dropping tests

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9284   Accepted: 3254

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 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

题意:

给了这么多对数,要求去掉k个,问能得到的最大数是多少;

思路:

01分数规划,就是二分答案,再check,check的过程中要排序,poj的输出真是蛋疼;
据说还有一种迭代的写法,上次做了一个数论迭代的题,不管开始的数是多少最后都能迭代出答案,好神奇啊;

Ac代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef  long long LL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e3+10;
const int maxn=1005;
const double eps=1e-10;

int n,k;
double mid;
struct node
{
    double a,b;
}po[N];
int cmp(node x,node y)
{
   return  x.a-mid*x.b<y.a-mid*y.b;
}
int check(double x)
{
    sort(po+1,po+n+1,cmp);
    double suma=0,sumb=0;
    for(int i=k+1;i<=n;i++)
    {
        //suma+=po[i].a-po[i].b*x;
       suma=suma+po[i].a*1.0;
        sumb=sumb+po[i].b*1.0;
    }
    if(suma>=sumb*x)return 1;
    return 0;
}

int main()
{
    while(1)
    {
        read(n);read(k);
        if(n==0&&k==0)break;
        For(i,1,n)scanf("%lf",&po[i].a);
        For(i,1,n)scanf("%lf",&po[i].b);
        double l=0,r=1.0;
        while(r>1e-5+l)
        {
            mid=(l+r)/2;
            if(check(mid))l=mid;
            else r=mid;
        }
        printf("%.0f
",100*l);
    }
        return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5663235.html