Adrenaline Rush

시간 제한3초메모리 제한2048 MB

요약
경주가 끝난 뒤의 자동차 최종 순서가 주어질 때, 각 쌍이 최대 한 번만 자리를 바꾸는 조건에서 시작 순서를 최종 순서로 바꾸는 최대 인접 교환 횟수와 그 순서를 구한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 구간
정답자
아직 제출이 없습니다

문제

Alice's friend is a big fan of the Adrenaline Rush racing competition and always strives to attend every race. However, this time, Alice is the one watching the race. To ensure her friend does not miss any important details, Alice decides to take notes on everything that happens on the track.

The first thing Alice notices before the race begins is the numbering of the cars. All the cars line up in front of the starting line in a specific order. The car closest to the line is numbered 11, the second car is numbered 22, and so on, up to the last car, which is numbered nn. How convenient! --- Alice thought.

The race begins with the countdown: "Three! Two! One! Go!". Alice observes that the cars start in their original order. However, as the race progresses, their order changes. She records whenever one car overtakes another, essentially swapping places with it on the track.

During the race, Alice notices something curious: no car overtakes another more than once. In other words, for any two cars xx and yy, there are at most two overtakes between them during the race: "xx overtakes yy" and/or "yy overtakes xx".

At the end of the race, Alice carefully writes down the final order of the cars c_1,c_2,…,c_nc\_1, c\_2, \ldots, c\_n, where c_1c\_1 represents the winner of the race.

Alice's friend, however, is only interested in the final ranking and discards all of Alice's notes except for the final ordering. As Alice is quite curious, she wonders: What is the longest possible sequence of overtakes she could have observed during the race? Your task is to help Alice answer this question.

입력

The first line of the input contains a single integer n(1≤n≤1000)n(1 \le n \le 1000) --- the number of cars in the race.

The second line contains a permutation c_1,c_2,…,c_n(1≤c_i≤n,c_i≠c_j)c\_1, c\_2, \ldots, c\_n(1 \le c\_i \le n, c\_i \ne c\_j) --- the final order of the cars.

출력

The first line of the output should contain a single integer mm --- the maximum possible number of overtakes that can occur during the race.

Each of the next mm lines should contain two integers xx and yy (1≤x,y≤n1 \le x, y \le n, x≠yx \ne y) representing an overtake event, where car xx overtakes car yy. This means that car xx was directly behind car yy and overtakes it. The overtakes must be listed in the order they occurred during the race.

After all mm overtakes have occurred, the cars must arrive at the finish line in the order c_1,c_2,…,c_nc\_1, c\_2, \ldots, c\_n. Note that any car xx should not overtake another car yy more than once.

If there are multiple possible longest sequences of overtakes, output any of them.

예제3

  1. 예제 1

    입력
    3
    2 3 1
    
    예상 출력
    4
    2 1
    3 1
    3 2
    2 3
    
  2. 예제 2

    입력
    1
    1
    
    예상 출력
    0
    
  3. 예제 3

    입력
    2
    1 2
    
    예상 출력
    2
    2 1
    1 2