Flag Rotation

시간 제한2초메모리 제한1024 MB

요약
줄무늬 순서가 주어질 때 각 행이 그 순서를 따르는 n x n 깃발과 각 열이 그 순서를 따르는 회전된 깃발에서 서로 다른 칸의 수를 센다.
난이도

보통10점 중 5점

유형
배열, 수학, 해시맵
정답자
아직 제출이 없습니다

문제

A common pattern for country flags is three stripes. Some flags are really similar, and have exactly the same stripes in the same order, but the flag is rotated by 9090 degrees. Using a sequence of colours, we can make a flag on a square grid, covering each row with the colours in the sequence. Another way to make a similar flag (that is rotated by 9090 degrees), is by covering each column with the colours in the sequence.

In preparation for the Flags You Paint for Countries meeting, you notice a mistake for one of the flags: it should have been rotated counter-clockwise by 9090 degrees! The square canvas is already stuck to a wall using super glue, so you can not simply take it off and rotate it. Therefore, you want to know how much work it would be to repaint one flag to the other. In one second, you can repaint one of the 1×11 \times 1 squares to any colour. What is the minimum number of seconds you need to repaint the flag? As an example, for the flag in the first sample input (shown in Figure F.1), 3434 squares need to be repainted.

Figure F.1: Two flags corresponding to the first sample input. Colour 11 corresponds to yellow, 22 to red, 33 to white, and 10001000 to blue. When the first flag should be rotated counter-clockwise by 9090 degrees without actually rotating the canvas, 3434 squares need to be repainted.

입력

The input consists of:

  • One line with an integer nn (1≤n≤2⋅1051\leq n\leq 2 \cdot 10^5), the number of stripes in the flag.
  • One line with nn integers aa (1≤a≤1091\leq a\leq 10^9), the colours of the stripes.

출력

Output the number of cells that need to be repainted when the flag should be rotated counter-clockwise by 9090 degrees.

예제3

  1. 예제 1

    입력
    7
    1 2 2 3 1000 2 1
    
    예상 출력
    34
    
  2. 예제 2

    입력
    4
    10 12 12 10
    
    예상 출력
    8
    
  3. 예제 3

    입력
    5
    6 5 5 6 5
    
    예상 출력
    12