P4377

求选中最大

并且$\sum b[i]\geq w$

01分数规划模版题。

考虑$01$规划,二分。$\sum \frac{b[i]}{a[i]}\geq ans\rightarrow \sum {b[i]}-mid\times {a[i] }\geq 0$

设$c[i]=\sum {b[i]}-mid\times {a[i] }$,做个背包就可以判断第二个条件

代码
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-6;
int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
double dp[N], c[N];
int a[N], b[N];
int n, W;
bool check(double x)
{
for (int i = 1; i <= n; i++)
c[i] = double(b[i]) - x * double(a[i]);

for (int i = 0; i <= W; i++)
dp[i] = -1e9;
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = W; j >= 0; j--)
{
if (j + a[i] >= W)
dp[W] = max(dp[W], dp[j] + c[i]);
else
dp[j + a[i]] = max(dp[j + a[i]], dp[j] + c[i]);
}
}

return dp[W] >= 0;
}
int main()
{
n = read(), W = read();
for (int i = 1; i <= n; i++)
a[i] = read(), b[i] = read();
double l = 0, r = 1e6;
while (l + eps < r)
{
double mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}

cout << (int)(l * 1000) << endl;
}