[01分数规划]Dropping tests POJ - 2976

题目:Dropping tests POJ - 2976

分析:

首先这题显然是兹磁二分的,而∑ai/∑bi≥x就等价于∑ai−x∑bi≥0。
所以我们发现二分完把ai-x*bi排序后把最大的n-k个选出来就行了。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int n, k;
ll a[maxn], b[maxn];
double c[maxn];

bool judge(double now)
{
    for(int i = 1; i <= n; i++)
        c[i] = a[i] - now * b[i];
    sort(c + 1, c + 1 + n);
    double sum = 0;
    for(int i = n; i >= n - k + 1; i--)
        sum += c[i];
    return sum >= 0;
}

int main()
{
    while(cin >> n >> k, n || k)
    {
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 1; i <= n; i++) cin >> b[i];
        k = n - k;
        double l = 0, r = 1e12, mid;
        while(l + eps <= r)
        {
            mid = (l + r) / 2;
            if(judge(mid)) l = mid;
            else r = mid;
        }
        printf("%lld\n", (ll)(r * 100 + 0.5));
    }
}

上一篇:EGL环境创建


下一篇:android-eglCreateWindowSurface:native_window_api_connect失败