cf——C. Insertion Sort

Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order.

for (int i = 1; i < n; i = i + 1)
{
   int j = i; 
   while (j > 0 && a[j] < a[j - 1])
   {
      swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1]
      j = j - 1;
   }
}

Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.

It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive — the actual permutation.

Output

Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions.


Sample test(s)
input
5
4 0 3 1 2
output
3 2
input
5
1 2 3 4 0
output
3 4
Note

In the first sample the appropriate pairs are (0, 3) and (0, 4).

In the second sample the appropriate pairs are (0, 4)(1, 4)(2, 4) and (3, 4).

题意:

已知一个无序的序列,使用冒泡排序对其进行排序,需要进行一定的SWAP()操作次数。

现在,让你从这个序列任意选择两个数进行交换,交换后再进行冒泡排序,使得交换后进行的冒泡排序中SWAP的操作

尽可能的少。

样例1解析:
4 0 3 1 2进行冒泡排序需要进行6次SWAP操作。
如果交换4,2,那么2 0 3 1 4只需要进行3次SWAP操作。
交换4,1,那么1 0 3 4 2也只需要进行3次SWAP操作。
交换之后需要进行3次SWAP操作,一共有2种交换方式,所以输出3 2.
做法:

num[i][j]=k:对于元素i,数组位于j后的所有元素,有k个小于i的数

然后枚举交换任意两个元素,SWAP的操作次数.

sum代表未交换元素时,SWAP的操作次数。

对于交换第i个和第j个元素(i>j,a[i]<a[j])

    int aa,bb,cc,dd,nt;
            aa=num[a[i]][j]-num[a[i]][i];
            bb=num[a[j]][j]-num[a[j]][i];
            cc=bb-aa;
            dd=(i-j-1)-bb;
            nt=sum+aa-cc-(bb+1);

aa代表从i到j,比a[i]小的数的个数

cc代表从i到j,比a[i]大,比a[j]小的数的个数

dd代表从i到j,比a[j]大的数的个数

nt代表交换元素i,j后,再进行冒泡排序,SWAP的次数



#include<iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[5005];
int pos[5005];
int num[5005][5005];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        pos[a[i]]=i;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=n-1;j>=0;j--)
        {
            if(a[j]<i)
            num[i][j]++;
            num[i][j]+=num[i][j+1];
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=num[i][pos[i]];
    }
    int Max=sum,ans=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<i;j++)
        {
            if(a[i]>a[j])
            continue;
            int aa,bb,cc,dd,nt;
            aa=num[a[i]][j]-num[a[i]][i];
            bb=num[a[j]][j]-num[a[j][i];
            cc=bb-aa;
            dd=(i-j-1)-cc;
            nt=sum+aa-cc-(bb+1);
            if(nt<Max)
            {
                Max=nt;
                ans=1;
            }
            else if(nt==Max)
            {
                ans++;
            }
        }
    }
    cout<<Max<<" "<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值