POJ_2299_Ultra-QuickSort & NYOJ_117_求逆序数

来源:互联网 发布:淘宝拍卖书画是真的吗 编辑:程序博客网 时间:2024/05/22 04:33
Ultra-QuickSort
Time Limit: 7000MS        Memory Limit: 65536K
Total Submissions: 60120        Accepted: 22265

Description
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,

Ultra-QuickSort produces the output
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

题意:求逆序数

思路:归并排序利用分治的思想,先把一个数组分成一个个序列,然后对一个个序列排序,把排好序的序列,在合并到原来的数组中。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAX = 500000+10;int s[MAX],arr[MAX];__int64 sum;void Merge(int a[],int l,int r){if(l<r){int mid = (l+r)/2;Merge(a,l,mid);    //划分成一个个小序列 Merge(a,mid+1,r);int i = l, j = mid+1,k = l;while(i<=mid && j<=r){    //对小序列排序 if(a[i]<=a[j]){arr[k++] = a[i++];}else{sum += j-k;arr[k++] = a[j++];}}while(i<=mid){arr[k++] = a[i++];}while(j<=r){arr[k++] = a[j++];}for(i=l;i<=r;i++){ //合并到原来的数组中 a[i] = arr[i];}}}int main(){int n;while(scanf("%d",&n) && n!=0){sum = 0;;for(int i=0;i<n;i++){scanf("%d",&s[i]);} Merge(s,0,n-1);printf("%lld\n",sum);}return 0;} 


求逆序数
时间限制:2000 ms  |  内存限制:65535 KB
难度:5
描述
    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。
    一个排列中逆序的总数就称为这个排列的逆序数。

    现在,给你一个N个元素的序列,请你判断出它的逆序数是多少。

    比如 1 3 2 的逆序数就是1。

    输入
        第一行输入一个整数T表示测试数据的组数(1<=T<=5)
        每组测试数据的每一行是一个整数N表示数列中共有N个元素(2〈=N〈=1000000)
        随后的一行共有N个整数Ai(0<=Ai<1000000000),表示数列中的所有元素。

        数据保证在多组测试数据中,多于10万个数的测试数据最多只有一组。
    输出
        输出该数列的逆序数
    样例输入

2
2
1 1
3
1 3 2

样例输出

0
1

#include <stdio.h>long long s[1000001];long long a[1000001];long long sum;void Merge(long long a[],int l,int mid,int r){int i = l,j = mid+1, k = l;while(i<=mid && j<=r){if(a[i]<=a[j]){s[k++] = a[i++];}else{sum += j - k;       s[k++] = a[j++];}}while(i<=mid){s[k++] = a[i++];}while(j<=r){s[k++] = a[j++];}for(i=l;i<=r;i++){a[i] = s[i];}}void sort(long long a[],int l,int r){if(l<r){int mid = (l+r)/2;sort(a,l,mid);sort(a,mid+1,r);Merge(a,l,mid,r);}}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);int i;for(i=0;i<n;i++){scanf("%d",&a[i]);}sum = 0;sort(a,0,n-1);printf("%lld\n",sum);}return 0;}





0 0
原创粉丝点击