URAL - In the Army Now(归并排序求逆序数对)

本文介绍了一个算法问题的解决方法,该问题要求找出一组数列中逆序数对最多的序列。通过使用归并排序算法,文章提供了一种有效的解决方案,并附带了完整的C++实现代码。

点击打开题目链接

1090. In the Army Now

Time limit: 1.0 second
Memory limit: 64 MB
The sergeant ordered that all the recruits stand in rows. The recruits have formed  K rows with  N people in each, but failed to stand according to their height. The right way to stand in a row is as following: the first soldier must be the highest, the second must be the second highest and so on; the last soldier in a row must be the shortest. In order to teach the young people how to form rows, the sergeant ordered that each of the recruits jump as many times as there are recruits before him in his row who are shorter than he. Note that there are no two recruits of the same height.
The sergeant wants to find which of the rows will jump the greatest total number of times in order to send this row to work in the kitchen. Help the sergeant to find this row.

Input

The first line of the input contains two positive integers  N and  K (2 ≤  N ≤ 10000, 1 ≤  K ≤ 20). The following  K lines contain  N integers each. The recruits in each row are numbered according to their height (1 — the highest,  N — the shortest). Each line shows the order in which the recruits stand in the corresponding row. The first integer in a line is the number of the first recruit in a row and so on. Therefore a recruit jumps as many times as there are numbers which are greater than his number in the line before this number.

Output

You should output the number of the row in which the total amount of jumps is the greatest. If there are several rows with the maximal total amount of jumps you should output the minimal of their numbers.

Sample

inputoutput
3 3
1 2 3
2 1 3
3 2 1
3
Problem Author: Nikita Shamgunov
Problem Source: USU Open Collegiate Programming Contest March'2001 Senior Session 

题目大意:

给出k组数,每组数n个,求出逆序数对最大的组标号。

思路:

树状数组可以用来求逆序数。同样,归并排序的一个比较重要的应用也是求解逆序数对。逆序数对 = 交换次数。

浅析各类排序算法(八) 归并排序

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 10000 + 5;
int arr[maxn], tmp[maxn];
int ans;

void Merge(int low, int mid, int high) { 
    int p_left = low, p_right = mid + 1, p_result = low;
    while(p_left <= mid && p_right <= high) {
        if(arr[p_left] > arr[p_right]) { 
            tmp[p_result++] = arr[p_right++];
            ans += mid - p_left+1;
        }
        else {
            tmp[p_result++] = arr[p_left++];
        }
    }
    while(p_left <= mid) 
        tmp[p_result++] = arr[p_left++];
    while(p_right <= high)
        tmp[p_result++] = arr[p_right++];
    for(int i = low; i <= high; i++) {
        arr[i] = tmp[i];
    }
}

void MergeSort(int low, int high) {
    if(low < high) { 
        int mid = (low + high) >> 1;
        MergeSort(low, mid);
        MergeSort(mid+1, high);
        Merge(low, mid, high);
    }
}

int main() {
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    int temp = 0, tmpid = 1;
    for(int i = 0; i < k; i++) {
        ans = 0;
        memset(arr,0,sizeof(arr));
        for(int j = 1; j <= n; j++) 
            cin >> arr[j];
        MergeSort(1, n);
        if(ans > temp) {
            temp = ans;
            tmpid = i + 1;
        }
    }
    cout << tmpid << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值