Orig Description
Problem J: Avant-garde Art
ICPC World Finals Day 6
Russian Constructivism is an art movement that started in the mid-1910s in the Soviet Union.
Mr. T, who had been staying in country R for a long time, was inspired by such things and decided to create a cool design even during the rehearsal of the ICPC World Finals.
Mr. T says, "Only circles and line segments are enough for symbols. It is important how beautifully line segments intersect."
Problem
Allocate \(n\) coordinates \(1, 2, \ldots, n\) equally spaced on the circumference.
Exactly one line segment emanates from each coordinate, and the line segment from coordinate \(i\) is connected to a different coordinate \(a_i\).
(Conversely, the line segment from coordinate \(a_i\) is connected to coordinate \(a_{a_i} = i\).) Up to \(k\) line segments can be freely reconnected on the circumference, regardless of the coordinates or lengths. Determine the maximum size of the set of intersecting line segments.
Input
n k
a1 a2 … an
On the first line, the number of coordinates \(n\) and the number of line segments that can be reconnected \(k\) are given separated by a space.
On the second line, the coordinate \(a_i\) that connects to the line segment at coordinate \(i\) is given separated by a space.
Output
After reconnecting at most \(k\) line segments,
output the maximum size of a set of intersecting line segments on a single line.
Constraints
\(2 \leq n \leq 8000\)
\(n\) is an even number.
\(0 \leq k \leq \min(n/2, 20)\)
\(1 \leq a_i \leq n\)
\(a_i \neq i\)
It is impossible to connect a line segment to itself.
\(a_i \neq a_j (i \neq j)\)
More than two line segments cannot be connected to the same coordinate.
\(a_{a_i} = i\)
If a line segment is connected from \(i\) to \(j\), then a line segment is connected from \(j\) to \(i\).
Sample Input/Output
Input 1
8 0
7 4 6 2 8 3 1 5
Output 1
2
The intersecting line segments are represented by the red line segments in the figure below.
Input 2
8 1
7 4 6 2 8 3 1 5
Output 2
3
By reconnecting the line segment connecting 1 and 7 as shown in the figure below, three intersecting line segments can be obtained.
Input 3
8 0
5 6 7 8 1 2 3 4
Output 3
4
Since all line segments intersect with each other, the maximum number of intersecting line segments is 4.
Extracted Specification
An integer \( n \) (2 ≤ \( n \) ≤ 8000, \( n \) is even), and an integer \( k \) (0 ≤ \( k \) ≤ min(\( n/2 \), 20)), followed by a list of integers \( a \) of size \( n \), where each integer \( a[i] \) (1 ≤ \( a[i] \) ≤ \( n \)) is unique and \( a[i] \neq i \).
### Example Input:
```
8 0
7 4 6 2 8 3 1 5
```
### Function Signature:
Write a function f(n, k, a) that takes in the input.
```python
def f(n: int, k: int, a: List[int]):
'''
n: an integer
k: an integer
a: a list of integers
'''
```
Test Case Generator
Sure, I can help you write a test case generator for the given problem "Avant-garde Art". The goal is to generate random, valid inputs according to the specified constraints.
Here's a Python function to generate test cases for this problem:
```python
import random
def generate_permutation(n):
"""
Generate a valid permutation where a_i != i and a_{a_i} = i.
"""
p = list(range(1, n+1))
while True:
random.shuffle(p)
valid = True
for i in range(n):
if p[i] == i+1 or p[p[i]-1] != i+1:
valid = False
break
if valid:
return p
def tcgen():
n = random.choice([i for i in range(2, 8001, 2)]) # n must be an even number between 2 and 8000
k = random.randint(0, min(n//2, 20)) # k is between 0 and min(n/2, 20)
a = generate_permutation(n)
return n, k, a
# Example usage
if __name__ == "__main__":
for _ in range(5): # Generate 5 test cases
n, k, a = tcgen()
print(n, k)
print(' '.join(map(str, a)))
```
### Explanation:
1. **generate_permutation(n)**:
- This function generates a valid permutation of size `n` where no element is mapped to itself and the condition `a_{a_i} = i` is satisfied. This is done by shuffling the list and checking the conditions.
2. **tcgen()**:
- `n` is chosen as a random even number between 2 and 8000.
- `k` is chosen as a random integer between 0 and `min(n/2, 20)`.
- The valid permutation `a` is generated using the `generate_permutation(n)` function.
3. **Example usage**:
- The loop generates 5 random test cases and prints them.
This generator ensures that the input constraints and properties are respected, producing valid test cases for the problem.
Extract Arguments
def extract_arguments(fh):
n, k = map(int, fh.readline().strip().split())
a = list(map(int, fh.readline().strip().split()))
return n, k, a