forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
57 lines (48 loc) · 984 Bytes
/
J.cpp
File metadata and controls
57 lines (48 loc) · 984 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int m, n, a[200200], b[200200];
int ok(int target)
{
int i = 0, j = target - 1, level = 0, socket = 1;
while (socket <= j)
{
while (j >= 0 && b[j] == level)
if (!socket) return 0;
else
{
socket--;
j--;
}
int plugUse = min(socket, m - i);
j -= socket - plugUse;
socket = 0;
while (plugUse--)
socket = min(n, socket + a[i++]);
level++;
}
return 1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
for (int i = 0; i < m; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
sort(a, a + m, greater<int>());
sort(b, b + n, greater<int>());
int low = 1, high = n, ans = 0;
while (low <= high)
{
int mid = (low + high) / 2;
if (ok(mid))
{
ans = mid;
low = mid + 1;
}
else high = mid - 1;
}
cout << ans << endl;
}