#P1053. [NOIP 2005 提高组] 篝火晚会
[NOIP 2005 提高组] 篝火晚会
Description
Jiajia has just entered high school. During military training, because Jiajia is hardworking and resilient, she quickly earned the instructor’s appreciation and became a “junior instructor” (xiao jiaoguan). On the last night of the training, Jiajia was ordered to organize a bonfire party. There are students, numbered from to . Initially, the students sit in a circle in the order . In reality, each person has two classmates they most wish to sit next to. How should Jiajia issue commands to rearrange the students into a new circle that satisfies everyone’s wishes? This is a big challenge for Jiajia.
Jiajia can issue commands, each of the following form:
Here, the value of is determined by Jiajia and can be different for each command. This command moves the positions of the students with numbers : moves to ’s position, moves to ’s position, …, and moves to ’s position. Executing each command incurs a cost. We assume that if a command moves people, then its cost is . We need Jiajia to achieve everyone’s wishes with the minimum total cost. Can you help Jiajia?
Input Format
The first line contains an integer , the total number of students.
The next lines each contain distinct positive integers, separated by a space. The -th of these lines gives the numbers of the two classmates whom student most wishes to sit next to.
Output Format
Output a single integer: the minimum total cost. If it is impossible to satisfy every student’s wish no matter how you rearrange, output .
4
3 4
4 3
1 2
1 2
2
Hint
- For of the testdata, .
- For of the testdata, .
Source: NOIP 2005 Senior, Problem 3.
Translated by ChatGPT 5
京公网安备 11011102002149号