Codeforces Round #212 (Div. 2) C. Insertion Sort

本文介绍了一个初学者程序员Petya如何使用插入排序算法,并通过预先选择一对元素进行交换来减少排序过程中swap函数的调用次数。文章提供了具体的实现代码及样例输入输出。

这道题的题意是给你一个数字n然后再给你n个数字,他们都是0-n-1的数字。求这n个数字,再交换任意两个数字之后再进行冒泡排序需要交换数字的次数。

首先肯定不是暴力、、、一开始没做出来,是鹏哥教的我、、、、太笨了啊,不解释啊、、、

大家还是看他的吧、、、http://blog.youkuaiyun.com/rowanhaoa/article/details/16341489

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.

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

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#define MAX 5050

using namespace std;

int num[MAX][MAX];
int dp[MAX];
int wei[MAX];

int main()
{
    int n, i, j;
    cin >>n;
    for(i = 0; i < n; i++)
    {
        cin >>dp[i];
        wei[dp[i]] = i;
    }
    for(i = 0; i < n; i++)
    {
        for(j = n-1; j >= 0; j--)
        {
            if(dp[j] < i)
                num[i][j] ++;
            num[i][j] += num[i][j+1];
        }
    }
    int sum = 0;
    for(i = 0; i < n; i++)
        sum += num[i][wei[i]];
    int _max = sum;
    int cnt;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < i; j++)
        {
            if(dp[i] > dp[j])
                continue;
            int x, y, z;
            x = num[dp[i]][j] - num[dp[i]][i];
            y = num[dp[j]][j] - num[dp[j]][i];
            z = y-x;
            int sum1 = sum+x-z-(y+1);
            if(sum1 < _max)
            {
                _max = sum1;
                cnt = 1;
            }
            else if(sum1 == _max)
            {
                cnt++;
            }
        }
    }
    cout <<_max<<' '<<cnt<<endl;
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值