#FT0000147. Các đường đi cắt nhau trên vòng tròn (Circle Cross)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các đường đi cắt nhau trên vòng tròn (Circle Cross)
Circle Cross
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
There are cows with distinct identifiers through . Each cow crosses a circular road at exactly two distinct positions. The positions are all different. Reading clockwise around the circle yields a sequence of cow identifiers, in which each identifier appears exactly twice.
The two paths for different cows necessarily cross precisely when their endpoints alternate around the circle. Equivalently, between the two occurrences of one cow there is exactly one occurrence of the other cow. Count the number of unordered pairs of cows whose paths cross; count each pair once.
Input
The first line contains . The following integers (which may be on separate lines) are the cow IDs in clockwise order.
Output
Print one integer: the number of crossing cow pairs.
Subtasks
- Subtask 1 (20 points): ; the sequence has IDs in , each occurring exactly twice.
- Subtask 2 (30 points): ; the sequence has IDs in , each occurring exactly twice.
- Subtask 3 (50 points): ; the sequence has IDs in , each occurring exactly twice.
Examples
Example 1
Input
4
3
2
4
4
1
3
2
1
Output
3
Explanation
The occurrence positions are cow 1: , cow 2: , cow 3: , and cow 4: . Alternating endpoint pairs are , and . Cow 4 has adjacent endpoints and crosses nobody, yielding 3 pairs.
Example 2
Input
3
1
2
1
3
2
3
Output
2
Explanation
Cows 1 and 2 have endpoints and , so these endpoints alternate. Cows 2 and 3 have and , also alternating. Cows 1 and 3 do not alternate. Hence exactly 2 crossing pairs.