【POJ 2976】 Dropping Tests

【题目链接】

           http://poj.org/problem?id=2976

【算法】

           0/1分数规划

【代码】

           

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1010
const double eps = 1e-4;

int i,n,k;
double l,r,mid,ans;
long long a[MAXN],b[MAXN];
double d[MAXN];

inline bool cmp(double a,double b)
{
        return a > b;
}
inline bool check()
{
        int i;
        double sum = 0;
        sort(d+1,d+n+1,cmp);
        for (i = 1; i <= n - k; i++) sum += d[i];
        return sum >= 0; 
}
    
int main() 
{
        
        while (scanf("%d%d",&n,&k) != EOF && !(n == 0 && k == 0))
        {
                for (i = 1; i <= n; i++) scanf("%lld",&a[i]);
                for (i = 1; i <= n; i++) scanf("%lld",&b[i]);
                l = 0; r = 1000;
                ans = 0;
                while (r - l > eps)
                {
                        mid = (l + r) / 2.0;
                        for (i = 1; i <= n; i++) d[i] = 1.0 * a[i] - mid * b[i];
                        if (check())
                        {
                                l = mid;
                                ans = mid;
                        } else r = mid;
                }              
                printf("%lld\n",(long long)(ans*100+0.5));
        }
        
        return 0;
    
}

 

posted @ 2018-07-11 21:22  evenbao  阅读(106)  评论(0编辑  收藏  举报