E. The Humanoid
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
There are n� astronauts working on some space station. An astronaut with the number i� (1≤i≤n1≤�≤�) has power ai��.
An evil humanoid has made his way to this space station. The power of this humanoid is equal to hℎ. Also, the humanoid took with him two green serums and one blue serum.
In one second , a humanoid can do any of three actions:
- to absorb an astronaut with power strictly less humanoid power;
- to use green serum, if there is still one left;
- to use blue serum, if there is still one left.
When an astronaut with power ai�� is absorbed, this astronaut disappears, and power of the humanoid increases by ⌊ai2⌋⌊��2⌋, that is, an integer part of ai2��2. For example, if a humanoid absorbs an astronaut with power 44, its power increases by 22, and if a humanoid absorbs an astronaut with power 77, its power increases by 33.
After using the green serum, this serum disappears, and the power of the humanoid doubles, so it increases by 22 times.
After using the blue serum, this serum disappears, and the power of the humanoid triples, so it increases by 33 times.
The humanoid is wondering what the maximum number of astronauts he will be able to absorb if he acts optimally.
Input
The first line of each test contains an integer t� (1≤t≤1041≤�≤104) — number of test cases.
The first line of each test case contains integers n� (1≤n≤2⋅1051≤�≤2⋅105) — number of astronauts and hℎ (1≤h≤1061≤ℎ≤106) — the initial power of the humanoid.
The second line of each test case contains n� integers ai�� (1≤ai≤1081≤��≤108) — powers of astronauts.
It is guaranteed that the sum of n� for all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, in a separate line, print the maximum number of astronauts that a humanoid can absorb.
Example
input
8
4 1
2 1 8 9
3 3
6 2 60
4 5
5 1 100 5
3 2
38 6 3
1 1
12
4 6
12 12 36 100
4 1
2 1 1 15
3 5
15 1 13
output
4
3
3
3
0
4
4
3
Note
In the first case, you can proceed as follows:
- use green serum. h=1⋅2=2ℎ=1⋅2=2
- absorb the cosmonaut 22. h=2+⌊12⌋=2ℎ=2+⌊12⌋=2
- use green serum. h=2⋅2=4ℎ=2⋅2=4
- absorb the spaceman 11. h=4+⌊22⌋=5ℎ=4+⌊22⌋=5
- use blue serum. h=5⋅3=15ℎ=5⋅3=15
- absorb the spaceman 33. h=15+⌊82⌋=19ℎ=15+⌊82⌋=19
- absorb the cosmonaut 44. h=19+⌊92⌋=23
AC:(c++)
Let's make two obvious remarks:
If we can absorb two astronauts with power x≤y
, then we can always first absorb an astronaut with power x
, and then an astronaut with power y
;
If we can absorb some astronaut, it is effective for us to do it right now.
Let's sort the astronauts powers in increasing order.
Now let's lock the sequence of serums we use. There are only three of them: blue serum can be the first, second or third.
Let's absorb the astronauts in increasing order of their powers, and if we can't, then use the next serum in a locked sequence or stop.
This solution works for O(n)
.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int n;
int arr[MAXN];
int solve(int i, long long h, int s2, int s3) {
if (i == n) return 0;
if (arr[i] < h)
return solve(i + 1, h + (arr[i] / 2), s2, s3) + 1;
int ans1 = (s2 ? solve(i, h * 2, s2 - 1, s3) : 0);
int ans2 = (s3 ? solve(i, h * 3, s2, s3 - 1) : 0);
return max(ans1, ans2);
}
int main() {
int t; cin >> t;
while(t--) {
long long h; cin >> n >> h;
for (int i = 0; i < n; ++i)
cin >> arr[i];
sort(arr, arr + n);
cout << solve(0, h, 2, 1) << endl;
}
}