Pair Sorting

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

요약
n개의 통에 n+1-i번 공이 두 개씩 들어 있을 때, 인접한 통 사이에서 공을 교환해 통 i에 i번 공 두 개가 오도록 만드는 교환 순서를 0.7n^2회 이내로 출력한다.
난이도

보통10점 중 7점

유형
그리디, 시뮬레이션, 구현, 정렬
정답자
아직 제출이 없습니다

문제

There are nn bins arranged in a row and 2n2n balls on the ground. The balls are numbered from 11 to nn and there are exactly two balls numbered ii, for each ii, 1≤i≤n1 ≤ i ≤ n. Also, for 1≤i≤n1 ≤ i ≤ n, the ii-th bin is denoted by B_iB\_i and each bin B_iB\_i can contain at most two balls. Initially, the bin B_iB\_i contains both of ball n+1−in + 1 - i’s, for 1≤i≤n1 ≤ i ≤ n. See the Figure F.1 below for the initial configuration of bins.

Figure F.1. The initial configuration of bins

You can swap two balls only from adjacent bins, which implies one swap operation. Note the bin is not a stack and for adjacent bins B_iB\_i and B_i+1B\_{i+1}, you can swap the one of two balls in B_iB\_i and the one in B_i+1B\_{i+1}. See the Figure F.2 below. The figure represents two swap operations.

Figure F.2. The swap operations between adjacent bins

Through these swap operations, you should sort the balls. As a result of the sorting, the bin B_iB\_i must contain the both of ball ii’s, for 1≤i≤n1 ≤ i ≤ n. In particular, the total number of swap operations should be no more than BoundBound, when BoundBound is given as a function of nn, especially, Bound=0.7n2Bound = 0.7n^2.

Given nn bins and 2n2n balls, write a program to find a sorting method of balls such that the total number of swap operations is no more than Bound=0.7n2Bound = 0.7n^2.

입력

Your program is to read from standard input. The input consists of exactly one line. The line contains an integer nn (3≤n≤1003 ≤ n ≤ 100), representing that there are nn bins and 2n2n balls.

출력

Your program is to write to standard output. Let SS be the total number of swap operations in your sorting method for the input. Print exactly S+1S + 1 lines. The first line contains SS. Each of the following SS lines contains three integers jj, aa, and bb, representing one swap operation between the ball aa in the bin B_jB\_j and the ball bb in B_j+1B\_{j+1}, where 1≤j≤n−11 ≤ j ≤ n - 1 and 1≤a,b≤n1 ≤ a, b ≤ n. The swap operations in your sorting method should be printed in order, one per line. The number SS must satisfy that S≤0.7n2S ≤ 0.7n^2.

예제2

  1. 예제 1

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

    입력
    3
    
    예상 출력
    5
    1 3 2
    2 3 1
    1 3 1
    2 3 1
    1 2 1