Orig Description
()
Problem Statement
Given a string S. Initially, S is an empty string.
You perform the following operation n times in order:
Add x_i occurrences of p_i (which is either "(" or ")") to the end of S.
Determine whether or not S becomes a balanced string after the operations.
A string is balanced if:
The empty string is balanced.
If a and b are balanced strings, then a + b is also balanced.
If a is a balanced string, then "(" + a + ")" is also balanced.
Input
The input is in the following format. All numbers given are integers.
n
p_1 x_1
. . .
p_n x_n
Constraints
1≦n≦1,000
1≦x_i≦10^6
p_i is either "(" or ")".
Output
If S becomes a balanced string, print "YES" on a line. Otherwise, print "NO" on a line.
Sample Input 1
3
( 5
) 4
) 1
Output for the Sample Input 1
YES
The string S="((((()))))" is balanced.
Sample Input 2
5
( 2
) 2
( 3
) 1
) 2
Output for the Sample Input 2
YES
Sample Input 3
2
) 1
( 1
Output for the Sample Input 3
NO
Extracted Specification
An integer n (1 ≤ n ≤ 1,000), representing some quantity or size.
A list of n tuples where each tuple contains a character and an integer. The character is either "(" or ")", and the integer is between 1 and 10^6.
### Example Input:
```
3
( 5
) 4
) 1
```
### Function Signature:
Write a function f(n, inputs) that takes in the input.
```python
def f(n: int, inputs: List[Tuple[str, int]]):
'''
n: an integer
inputs: a list of tuples, each containing a character and an integer
'''
```
Test Case Generator
Sure! Here's a test case generator for the given problem statement:
```python
import random
def tcgen():
# Number of operations, n
n = random.randint(1, 1000)
operations = []
for _ in range(n):
# Randomly choose between '(' and ')'
p = random.choice(['(', ')'])
# Randomly choose an integer x_i (1 ≤ x_i ≤ 10^6)
x = random.randint(1, 10**6)
operations.append((p, x))
return n, operations
# Function to print the test case in the required format
def print_test_case(n, operations):
print(n)
for p, x in operations:
print(p, x)
# Generate a test case and print it
if __name__ == "__main__":
n, operations = tcgen()
print_test_case(n, operations)
```
This generator will produce random test cases conforming to the constraints specified in the problem statement.
### Explanation of the Test Case Generator:
1. **Generate `n`**:
- Randomly choose an integer `n` between 1 and 1000.
2. **Generate Operations**:
- For each of the `n` operations:
- Randomly choose a character `p` which is either '(' or ')'.
- Randomly choose an integer `x` between 1 and \(10^6\).
3. **Print the Test Case**:
- The `print_test_case` function prints the test case in the required format.
- It first prints the number of operations `n`.
- Then, for each operation, it prints the character `p` and the integer `x`.
### Example Output:
Running the generator might produce an output like this:
```
3
( 1000000
) 500000
( 750000
```
This represents a test case where `n` is 3, and the operations are to add:
1. `1000000` occurrences of '('
2. `500000` occurrences of ')'
3. `750000` occurrences of '('
You can use this generator to create multiple test cases for testing the solution to the problem.
Extract Arguments
def extract_arguments(fh):
n = int(fh.readline().strip())
inputs = []
for _ in range(n):
p, x = fh.readline().strip().split()
x = int(x)
inputs.append((p, x))
return inputs