Orig Description
Score : 1200 points
Problem StatementSnuke is playing with red and blue balls, placing them on a two-dimensional plane.
First, he performed N operations to place red balls. In the i-th of these operations, he placed RC_i red balls at coordinates (RX_i,RY_i).
Then, he performed another N operations to place blue balls. In the i-th of these operations, he placed BC_i blue balls at coordinates (BX_i,BY_i).
The total number of red balls placed and the total number of blue balls placed are equal, that is, \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i. Let this value be S.
Snuke will now form S pairs of red and blue balls so that every ball belongs to exactly one pair.
Let us define the score of a pair of a red ball at coordinates (rx, ry) and a blue ball at coordinates (bx, by) as |rx-bx| + |ry-by|.
Snuke wants to maximize the sum of the scores of the pairs.
Help him by finding the maximum possible sum of the scores of the pairs.
Constraints
1 \leq N \leq 1000
0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9
1 \leq RC_i,BC_i \leq 10
\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i
All values in input are integers.
InputInput is given from Standard Input in the following format:
N
RX_1 RY_1 RC_1
RX_2 RY_2 RC_2
\vdots
RX_N RY_N RC_N
BX_1 BY_1 BC_1
BX_2 BY_2 BC_2
\vdots
BX_N BY_N BC_N
OutputPrint the maximum possible sum of the scores of the pairs.
Sample Input 12
0 0 1
3 2 1
2 2 1
5 0 1
Sample Output 18
If we pair the red ball at coordinates (0,0) and the blue ball at coordinates (2,2), the score of this pair is |0-2| + |0-2|=4.
Then, if we pair the red ball at coordinates (3,2) and the blue ball at coordinates (5,0), the score of this pair is |3-5| + |2-0|=4.
Making these two pairs results in the total score of 8, which is the maximum result.
Sample Input 23
0 0 1
2 2 1
0 0 2
1 1 1
1 1 1
3 3 2
Sample Output 216
Snuke may have performed multiple operations at the same coordinates.
Sample Input 310
582463373 690528069 8
621230322 318051944 4
356524296 974059503 6
372751381 111542460 9
392867214 581476334 6
606955458 513028121 5
882201596 791660614 9
250465517 91918758 3
618624774 406956634 6
426294747 736401096 5
974896051 888765942 5
726682138 336960821 3
715144179 82444709 6
599055841 501257806 6
390484433 962747856 4
912334580 219343832 8
570458984 648862300 6
638017635 572157978 10
435958984 585073520 7
445612658 234265014 6
Sample Output 345152033546
Extracted Specification
An integer N (1 ≤ N ≤ 1000), representing some quantity or size.
A list of tuples of three integers of size N, where each integer is between 0 and 10^9 for the first two integers and between 1 and 10 for the third integer.
Another list of tuples of three integers of size N, where each integer is between 0 and 10^9 for the first two integers and between 1 and 10 for the third integer.
### Example Input:
```
2
0 0 1
3 2 1
2 2 1
5 0 1
```
### Function Signature:
Write a function f(N, red_balls, blue_balls) that takes in the input.
def f(N: int, red_balls: List[Tuple[int, int, int]], blue_balls: List[Tuple[int, int, int]]):
'''
N: an integer
red_balls: a list of tuples of three integers
blue_balls: a list of tuples of three integers
'''
Test Case Generator
Sure, I can help you write a test case generator for this problem. This generator will create random values for \(N\), and the coordinates and counts for red and blue balls while ensuring that the total number of red and blue balls are equal.
Here is the Python code for the test case generator:
```python
import random
def generate_coordinates_and_counts(N):
coordinates_and_counts = []
total_counts = 0
for _ in range(N):
x = random.randint(0, 10**9)
y = random.randint(0, 10**9)
c = random.randint(1, 10)
coordinates_and_counts.append((x, y, c))
total_counts += c
return coordinates_and_counts, total_counts
def tcgen():
# Generate N
N = random.randint(1, 1000)
# Generate red balls data
red_data, red_total = generate_coordinates_and_counts(N)
# Generate blue balls data
blue_data, blue_total = generate_coordinates_and_counts(N)
# Adjust counts to make sure red_total == blue_total
while red_total != blue_total:
if red_total > blue_total:
excess = red_total - blue_total
for i in range(N):
if red_data[i][2] > 1:
adjust = min(red_data[i][2] - 1, excess)
red_data[i] = (red_data[i][0], red_data[i][1], red_data[i][2] - adjust)
red_total -= adjust
if red_total == blue_total:
break
else:
excess = blue_total - red_total
for i in range(N):
if blue_data[i][2] > 1:
adjust = min(blue_data[i][2] - 1, excess)
blue_data[i] = (blue_data[i][0], blue_data[i][1], blue_data[i][2] - adjust)
blue_total -= adjust
if blue_total == red_total:
break
# Print the test case
print(N)
for (rx, ry, rc) in red_data:
print(rx, ry, rc)
for (bx, by, bc) in blue_data:
print(bx, by, bc)
# Example usage:
tcgen()
```
This code generates a random integer \(N\) within the given constraints, creates random coordinates and counts for both the red and blue balls, and ensures that the total number of red and blue balls are equal.
Here's a step-by-step explanation of the test case generator:
1. **Generate \(N\)**: Randomly choose \(N\) between 1 and 1000.
2. **Generate Red Balls Data**: Generate \(N\) sets of coordinates and counts for the red balls, ensuring the counts sum up correctly.
3. **Generate Blue Balls Data**: Generate \(N\) sets of coordinates and counts for the blue balls, ensuring the counts sum up correctly.
4. **Adjust Counts**: Ensure that the total counts of red and blue balls are equal by adjusting the counts if necessary.
5. **Print Test Case**: Print \(N\), followed by the red balls' data and the blue balls' data.
You can run this code in your local Python environment to generate test cases for the given problem.
Extract Arguments
def extract_arguments(fh):
N = int(fh.readline().strip())
red_balls = []
for _ in range(N):
RX, RY, RC = map(int, fh.readline().strip().split())
red_balls.append((RX, RY, RC))
blue_balls = []
for _ in range(N):
BX, BY, BC = map(int, fh.readline().strip().split())
blue_balls.append((BX, BY, BC))
return N, red_balls, blue_balls
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# N, red_balls, blue_balls = extract_arguments(fh)
# f(N, red_balls, blue_balls)