Orig Description
Problem G: The Humans Braving the Invaders
Problem
Currently, Earth is under attack by invaders from space, and only we who are in the base, which is the only surviving humans, are left. There is little military power left to fight against them. Nevertheless, we do not give up here. Completely annihilating the Invader is the last means for humanity to survive. We must fight to the end as representatives of the remaining humanity. The following describes the content of the final operation that will take place now.
First, since our military power is far too low compared to that of the Invaders, we will take refuge in this base and conduct a siege battle. This base is surrounded by high mountains, and the only way for the Invaders to invade is to pass through the straight road in front of the base. Let's call this road a field. Due to this feature, it is possible to attack the Invaders concentratedly in front. We who are in the base attack the Invaders with two types of weapons. One is a sniper rifle that aims at one Invader. The other is a grenade launcher that can attack a wide range.
Your job as the only programmer among us humans is to simulate the battle based on the action records of the Invaders and us. The action record is given in query format. Each query consists of one or more integers and is given as follows:
0
The Invader appears at a position L distance from the base on the field.
1 d
All Invaders currently on the field move d closer to the base. After this action, Invaders that reach the base will receive damage, and those enemies will disappear from the field. If damage is received, output "damage (number of Invaders that reached the base)" on one line.
2 k
If the number of Invaders currently on the field is equal to or greater than k , attack the k th Invader from the base with a sniper rifle. The Invader will disappear from the field. Then output "hit" on one line. If the number of Invaders on the field is less than k , output "miss" on one line.
3 x r
Attack a range of r with a grenade launcher at a position x distance from the base. All Invaders on the field whose distance from the base is within r from the point of impact at a distance of x from the base will disappear from the field. Then output "bomb (number of Invaders defeated)" on one line. As a supplement, the base will not receive any damage from the grenade launcher.
4 k
If the number of Invaders on the field is equal to or greater than k , output "distance (distance between the k th Invader closest to the base and the base)" on one line. If the number of Invaders on the field is less than k , output "distance -1" on one line.
That's all for the explanation of the operation. All hands, start the operation! ...Good luck.
Input
The input consists of multiple data sets. Each data set is represented as follows. The first line gives two integers Q and L separated by a space. The following Q lines give the queries described above. The input ends with two zeros.
Constraints
The input satisfies the following conditions.
All values in the input are integers.
1 ≤ Q ≤ 100000
1 ≤ L ≤ 109
1 ≤ d , k ≤ 109
0 ≤ x ≤ L
0 ≤ r ≤ 109
There are no more than two Invaders at the same position.
There are no more than three data sets.
Output
For each data set, output according to the instruction of the query to be output. Output "end" at the end of each data set.
Sample Input
18 10
0
4 1
1 1
4 1
0
1 1
0
2 2
1 10
0
1 1
0
1 5
3 4 0
3 4 1
0
9 10
4 1
2 2
3 5 5
0
4 1
2 2
3 5 5
0
2 1
0 0
Sample Output
distance 10
distance 9
hit
damage 2
bomb 1
bomb 2
end
distance -1
miss
bomb 0
distance 10
miss
bomb 1
hit
end
Extracted Specification
An integer Q (1 ≤ Q ≤ 100000), and an integer L (1 ≤ L ≤ 10^9).
Q lines of queries, where each query is one of the following:
- "0"
- "1 d" where d is an integer (1 ≤ d ≤ 10^9)
- "2 k" where k is an integer (1 ≤ k ≤ 10^9)
- "3 x r" where x is an integer (0 ≤ x ≤ L) and r is an integer (0 ≤ r ≤ 10^9)
- "4 k" where k is an integer (1 ≤ k ≤ 10^9)
The input ends with two zeros "0 0".
### Example Input:
```
18 10
0
4 1
1 1
4 1
0
1 1
0
2 2
1 10
0
1 1
0
1 5
3 4 0
3 4 1
0
9 10
4 1
2 2
3 5 5
0
4 1
2 2
3 5 5
0
2 1
0 0
```
### Function Signature:
Write a function `f(inputs)` that takes in the input.
```python
def f(inputs: List[Tuple[int, int, List[Union[str, Tuple[int], Tuple[int, int], Tuple[int, int, int]]]]]):
'''
inputs: a list of tuples, where each tuple contains:
- an integer
- another integer
- a list of queries, where each query is one of the following:
- a string "0"
- a tuple (1, int)
- a tuple (2, int)
- a tuple (3, int, int)
- a tuple (4, int)
'''
```
Test Case Generator
Sure, here's a test case generator for the given problem "The Humans Braving the Invaders":
```python
import random
def generate_test_case():
# Generate a random number of data sets, not more than 3
num_datasets = random.randint(1, 3)
test_case = []
for _ in range(num_datasets):
# Generate Q (1 ≤ Q ≤ 100000) and L (1 ≤ L ≤ 10^9)
Q = random.randint(1, 100000)
L = random.randint(1, 10**9)
test_case.append(f"{Q} {L}")
queries = []
for _ in range(Q):
query_type = random.randint(0, 4)
if query_type == 0:
queries.append(f"0")
elif query_type == 1:
d = random.randint(1, 10**9)
queries.append(f"1 {d}")
elif query_type == 2:
k = random.randint(1, 10**9)
queries.append(f"2 {k}")
elif query_type == 3:
x = random.randint(0, L)
r = random.randint(0, 10**9)
queries.append(f"3 {x} {r}")
elif query_type == 4:
k = random.randint(1, 10**9)
queries.append(f"4 {k}")
test_case.extend(queries)
# Append the ending zeroes to signify the end of input
test_case.append("0 0")
return test_case
# Example of how to use the test case generator
test_case = generate_test_case()
for line in test_case:
print(line)
```
### Explanation:
1. **Number of Datasets**: The generator first determines how many datasets to generate (between 1 and 3).
2. **Q and L**: For each dataset, it generates `Q` (number of queries, between 1 and 100000) and `L` (field length, between 1 and 10^9).
3. **Queries**: For each query, it randomly selects a query type (0 to 4) and generates appropriate parameters:
- **Type 0**: No additional parameters.
- **Type 1**: Generates a random `d` (1 to 10^9).
- **Type 2**: Generates a random `k` (1 to 10^9).
- **Type 3**: Generates a random `x` (0 to `L`) and a random `r` (0 to 10^9).
- **Type 4**: Generates a random `k` (1 to 10^9).
4. **End of Input**: Finally, it appends "0 0" to signify the end of input.
This generator ensures that the generated test cases conform to the constraints provided in the problem statement.
Extract Arguments
def extract_arguments(fh):
data_sets = []
while True:
line = fh.readline().strip()
if line == "0 0":
break
Q, L = map(int, line.split())
queries = []
for _ in range(Q):
query = list(map(int, fh.readline().strip().split()))
queries.append(query)
data_sets.append((Q, L, queries))
return data_sets
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# data_sets = extract_arguments(fh)
# for data_set in data_sets:
# Q, L, queries = data_set
# f(Q, L, queries)