-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfence8.cpp
66 lines (59 loc) · 1.33 KB
/
fence8.cpp
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
66
/*
TASK:fence8
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, r, wood[55], need[1024], sum[1024], ans, space, tot;
int maxw[55];
bool dfs(int dep, int pos)
{
if (dep == 0) return true;
if (space > tot - sum[ans]) return false;
for (int i = pos; i <= n; ++i)
if (wood[i] >= need[dep])
{
wood[i] -= need[dep];
if (wood[i] < need[1]) space += wood[i];
bool flag = false;
if (need[dep] == need[dep - 1]) flag = dfs(dep - 1, i);
else flag = dfs(dep - 1, 1);
if (wood[i] < need[1]) space -= wood[i];
wood[i] += need[dep];
if (flag) return true;
}
return false;
}
int bs(int left, int right)
{
while (left < right)
{
ans = (left + right) / 2 + 1;
space = 0;
if (dfs(ans, 1)) left = ans;
else right = ans - 1;
}
return left;
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
freopen("fence8.in", "r", stdin);
freopen("fence8.out", "w", stdout);
scanf("%d", &n);
tot = 0;
for (int i = 1; i <= n; ++i) scanf("%d", &wood[i]), tot += wood[i];
sort(wood + 1, wood + n + 1, cmp);
scanf("%d", &r);
sum[0] = 0;
for (int i = 1; i <= r; ++i) scanf("%d", &need[i]);
sort(need + 1, need + r + 1);
for (int i = 1; i <= r; ++i) sum[i] = sum[i - 1] + need[i];
printf("%d\n", bs(0, r));
return 0;
}