Orig Description
Score : 800 points
Problem StatementTakahashi and Aoki will play a game using a grid with H rows and W columns of square cells.
There are N obstacles on this grid; the i-th obstacle is at (X_i,Y_i).
Here, we represent the cell at the i-th row and j-th column (1 \leq i \leq H, 1 \leq j \leq W) by (i,j).
There is no obstacle at (1,1), and there is a piece placed there at (1,1).
Starting from Takahashi, he and Aoki alternately perform one of the following actions:
Move the piece to an adjacent cell.
Here, let the position of the piece be (x,y). Then Takahashi can only move the piece to (x+1,y), and Aoki can only move the piece to (x,y+1).
If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken.
Do not move the piece, and end his turn without affecting the grid.
The game ends when the piece does not move twice in a row.
Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends.
How many actions will Takahashi end up performing?
Constraints
1 \leq H,W \leq 2\times 10^5
0 \leq N \leq 2\times 10^5
1 \leq X_i \leq H
1 \leq Y_i \leq W
If i \neq j, (X_i,Y_i) \neq (X_j,Y_j)
(X_i,Y_i) \neq (1,1)
X_i and Y_i are integers.
InputInput is given from Standard Input in the following format:
H W N
X_1 Y_1
:
X_N Y_N
OutputPrint the number of actions Takahashi will end up performing.
Sample Input 13 3 1
3 2
Sample Output 12
For example, the game proceeds as follows:
Takahashi moves the piece to (2,1).
Aoki does not move the piece.
Takahashi moves the piece to (3,1).
Aoki does not move the piece.
Takahashi does not move the piece.
Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends.
Sample Input 210 10 14
4 3
2 2
7 3
9 10
7 7
8 1
10 10
5 4
3 4
2 8
6 4
4 4
5 8
9 2
Sample Output 26
Sample Input 3100000 100000 0
Sample Output 3100000
Extracted Specification
An integer H (1 ≤ H ≤ 2×10^5), representing some quantity or size.
An integer W (1 ≤ W ≤ 2×10^5), representing some quantity or size.
An integer N (0 ≤ N ≤ 2×10^5), representing some quantity or size.
A list of tuples of size N, where each tuple (X_i, Y_i) contains two integers (1 ≤ X_i ≤ H, 1 ≤ Y_i ≤ W).
### Example Input:
```
3 3 1
3 2
```
### Function Signature:
Write a function f(H, W, N, obstacles) that takes in the input.
def f(H: int, W: int, N: int, obstacles: List[Tuple[int, int]]):
'''
H: an integer
W: an integer
N: an integer
obstacles: a list of tuples, each containing two integers
'''
Test Case Generator
Sure, I can help you with that. Let's start by understanding the requirements for generating a test case for the given problem statement:
1. We need to generate random values for \( H \) and \( W \) within the range [1, 200,000].
2. We need to generate a random number of obstacles \( N \) within the range [0, 200,000].
3. For each obstacle, generate random coordinates \( (X_i, Y_i) \) such that \( (X_i, Y_i) \neq (1, 1) \) and no two obstacles share the same coordinates.
Now, let's implement the test case generator in Python:
```python
import random
def generate_unique_obstacles(H, W, N):
obstacles = set()
while len(obstacles) < N:
x = random.randint(1, H)
y = random.randint(1, W)
if (x, y) != (1, 1):
obstacles.add((x, y))
return list(obstacles)
def tcgen():
H = random.randint(1, 200000)
W = random.randint(1, 200000)
N = random.randint(0, 200000)
obstacles = generate_unique_obstacles(H, W, N)
return H, W, N, obstacles
# Example usage
if __name__ == "__main__":
H, W, N, obstacles = tcgen()
print(H, W, N)
for x, y in obstacles:
print(x, y)
```
### Explanation:
1. **generate_unique_obstacles**: This function generates a set of unique obstacle coordinates. It ensures that each obstacle is unique and not placed at (1, 1).
2. **tcgen**: This is the main test case generator function. It generates random values for \( H \), \( W \), and \( N \). It then calls `generate_unique_obstacles` to generate the list of obstacles.
3. **Printing the output**: The generated values for \( H \), \( W \), \( N \), and obstacle coordinates are printed in the required format.
This script should generate valid and random test cases for the given problem statement. Feel free to run and test it as needed.
Extract Arguments
def extract_arguments(fh):
H, W, N = map(int, fh.readline().strip().split())
obstacles = []
for _ in range(N):
X, Y = map(int, fh.readline().strip().split())
obstacles.append((X, Y))
return H, W, N, obstacles
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# H, W, N, obstacles = extract_arguments(fh)
# f(H, W, N, obstacles)