Logo
(追記) (追記ここまで)

28319번 - Line Town 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB199844.444%

문제

The $N$ residents of Line Town have arranged themselves in a line. Initially, the residents have happiness values of $h_{1}, h_{2}, \ldots, h_{N}$ from left to right along the line.

Since you are the mayor of Line Town, you are implementing the third pillar of your plan entitled "Community, Candy, and Organization" (CCO). As such, you have taken the mayoral power to swap the residents' locations. In one swap, you may tell two adjacent residents to swap their positions in the line. However, this swap will cause both residents to negate their happiness values.

You would like to perform some swaps so that the residents' happiness values are in nondecreasing order from left to right in the line. Determine whether this is possible, and if so, the minimum number of swaps needed.

입력

The first line of input contains a single integer $N$.

The next line of input contains $N$ integers $h_{1}, \ldots, h_{N}$ $(-10^{9} \le h_{i} \le 10^{9}),ドル the happiness values of the residents from left to right.

출력

On a single line, output the minimum number of swaps, or -1 if the task is impossible.

제한

  • 1ドル \le N \le 500,000円$

예제 입력 1

6
-2 7 -1 -8 2 8

예제 출력 1

3

It is possible to perform 3ドル$ swaps as follows:

  1. Swap the 2ドル\text{nd}$ and 3ドル\text{rd}$ resident so that the line becomes $[-2, 1, -7, -8, 2, 8]$.
  2. Swap the 4ドル\text{th}$ and 5ドル\text{th}$ resident so that the line becomes $[-2, 1, -7, -2, 8, 8]$.
  3. Swap the 3ドル\text{rd}$ and 4ドル\text{th}$ resident so that the line becomes $[-2, 1, 2, 7, 8, 8]$.

The residents are now arranged in non-decreasing order of happiness values as required. No non-decreasing arrangement can be obtained with less than 3ドル$ swaps.

예제 입력 2

4
1 -1 1 -1

예제 출력 2

-1

There is no sequence of swaps that will place residents in non-decreasing order of happiness values.

힌트

출처

Olympiad > Canadian Computing Competition & Olympiad > 2023 > CCO 2023 3번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /