2023 ICPC 亚洲区域赛 济南站 I. Strange Sorting

I. Strange Sorting

time limit per test: 1 second

memory limit per test: 1024 megabytes

We present an extremely simple sorting algorithm.

It may look like it is obviously wrong,

but we prove that it is in fact correct. ∗

After learning the strange sorting algorithm in the problem Paimon Sorting of The 2021 ICPC Asia Nanjing Regional Contest, Little Cyan Fish comes up with the following task.

Given a sequence a1,a2,⋯,an which is a permutation of n, your task is to sort the permutation in ascending order by applying the following operation for at most ⌊n2⌋ times: Choose two indices l and r satisfying 1≤l<r≤n and al>ar, and then sort al,al+1,⋯,ar in ascending order.

Recall that a permutation of n is a sequence of length n, in which each integer from 1 to n (both inclusive) appears exactly once. Also recall that ⌊x⌋ indicates the largest integer less than or equal to x.


∗Stanley P. Y. Fung. Is this the simplest (and most surprising) sorting algorithm ever? arXiv:2110.01111

Input

There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:The first line contains an integer n (1≤n≤100) indicating the length of the permutation.

The second line contains n distinct integers a1,a2,⋯,an (1≤ai≤n) indicating the given permutation.

It's guaranteed that the sum of n of all test cases will not exceed 10^{4}.

Output

For each test case, first output one line containing one integer k (0≤k≤\left \lfloor \frac{n}{2} \right \rfloor) indicating the number of operations you're going to use. Then output k lines, where the i-th line contains two integers li and ri separated by a space, indicating the two indices you choose for the i-th operation.

It can be proven that the answer always exists. If there are multiple valid answers, you can output any of them.

Example

input

3

6

2 3 4 6 5 1

5

1 2 3 4 5

3

2 3 1

output

2

3 6

1 3

0

1

1 3

Note

For the first sample test case, after the 1-st operation the permutation becomes {2,3,1,4,5,6}, and after the 2-nd operation the permutation becomes {1,2,3,4,5,6}, which is in ascending order.

【思路分析】

数学+贪心。考虑序列2,1,4,3,6,5,...,显然每次两两sort所需次数不超过\left \lfloor \frac{n}{2} \right \rfloor。当n为奇数时,多余的元素不影响排序次数。将原数组分为多区间排序,该区间右端点需尽可能靠近原数组右侧,这样可以将更多的元素纳入排序范围。由于排序后区间元素可能大于区间右侧元素,将左端点右移进行下一次区间排序。

#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>
#include <set>
#include <queue>

#define i64 long long

using namespace std;

typedef pair<i64,i64> pii;

vector<pii> res;
i64 n,cnt;

void cal(i64 l,i64 arr[]){
    if (l>=n-1) return;
    bool ok = false;
    for (int i = n - 1; i >= l; --i) {
        if (arr[i]<arr[l]){
            sort(arr+l,arr+i+1);
            ok = true;
            res.emplace_back(l+1,i+1);
            ++cnt;
            cal(l+1,arr);
            break;
        }
    }
    if (!ok){
        cal(l+1,arr);
    }
}

void solve() {
    res.clear();
    cin>>n;
    cnt = 0;
    i64 arr[n];
    for (int i = 0; i < n; ++i) cin>>arr[i];
    cal(0,arr);
    cout<<cnt<<endl;
    for (const auto &item: res) cout<<item.first<<" "<<item.second<<endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值