poj 2976 Dropping tests

题目描述

01分数规划模板题

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 1010
#define cn const node
#define db double
#define eps 1e-4
using namespace std;

int n, k;

db mid;
struct node {
int x, y;

friend bool operator < (cn &u, cn &v) { return u.x - mid * u.y > v.x - mid * v.y; }
} a[maxn];

bool check() {
sort(a + 1, a + n + 1); db s = 0;
for (int i = 1; i <= n - k; ++i) s += a[i].x - mid * a[i].y;
return s >= 0;
}

void work() {
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].x);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].y);
db l = 0, r = 1, ans;
while (r - l > eps) {
mid = (r + l) / 2;
if (check()) l = mid, ans = mid;
else r = mid;
} printf("%.0f\n", ans * 100);
}

int main() {
while (cin >> n >> k) {
if (!n && !k) break;
work();
}
return 0;
}