URAL

来源:互联网 发布:activity之间数据传递 编辑:程序博客网 时间:2024/05/22 15:15

点击打开题目链接

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 31 2 32 1 33 2 1
3
Problem Author: Nikita Shamgunov
Problem Source: USU Open Collegiate Programming Contest March'2001 Senior Session 
Tags: data structures  (
hide tags for unsolved problems
)

题目大意:

给出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;}

原创粉丝点击