C. Insertion Sort----构造题

C. Insertion Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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)


题目链接:http://codeforces.com/contest/362/problem/C


这个题并没有想出来,看了大佬的题解,还需要加强这方面的练习啊

http://blog.csdn.net/u013912596/article/details/39005037


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
#define N 5005
using namespace std;
int mi[N][N],ma[N][N],num[N],cnt,ans=inf;
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&num[i]);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(num[j]>num[i])
                ma[i][j]=1;
            if(num[j]<num[i])
                mi[i][j]=1;
            if(j){
                ma[i][j]+=ma[i][j-1];
                mi[i][j]+=mi[i][j-1];
            }
        }
    }
    int cur=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i<j&&num[i]>num[j])
                cur++;
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(num[i]<num[j])
                continue;
            int temp=cur;
            temp+=(mi[j][j-1]-mi[j][i]+ma[i][j-1]-ma[i][i]);
            temp-=(ma[j][j-1]-ma[j][i]+mi[i][j-1]-mi[i][i]);
            temp--;
            if(ans==temp)
                cnt++;
            else if(ans>temp){
                ans=temp;
                cnt=1;
            }
        }
    }
    cout<<ans<<" "<<cnt<<endl;
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值