poj 2299 Ultra-QuickSort(求逆序对)&& poj 1804

本文介绍了一种名为Ultra-QuickSort的排序算法,并详细分析了其如何通过计算逆序对来确定最少的交换次数以完成排序。文章提供了两种实现方式:归并排序和树状数组,并附带完整代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=2299

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

Source

Waterloo local 2005.02.05

题目要求用归并排序求最少的交换次数,其实就是求逆序对的个数。。
第一种:
归并排序nlogn...(PS 最近在改代码风格,师兄说我之前写的太挫了...)
推荐一个归并排序算法讲解: http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html

代码:
#include <stdio.h>
#include <string.h>
const int maxn = 500005;
int a[maxn],b[maxn];
long long  ans = 0;
void mergesort(int low, int m, int high)
{
    int i = low, j = m + 1;
    int k = 0;
    while(i <= m && j <= high)  //左右两边比较,取较小的那个
        {
            if(a[i] <= a[j])
            {
                b[k++] = a[i++];
            }
            else
            {
                b[k++] = a[j++];
                ans += (long long)m - i + 1;  //统计逆序对的个数
            }
        }
    while(i <= m)           //左边不空
        b[k++] = a[i++];
    while(j <= high)          //右边不空
        b[k++] = a[j++];
    for(i = low, k = 0; i <= high; i++, k++)  //将排序后的放回原来数组里
        a[i] = b[k];
}
void merge(int low, int high)   //递归
{
    int mid;
    if(low < high)
    {
        mid = (low + high) / 2;
        merge (low, mid);
        merge (mid+1, high);
        mergesort(low, mid, high);  //分治
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int n;
    while(scanf("%d ", &n) && n)
    {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        ans = 0;
        for(int i = 1; i <= n; i++)
            scanf("%d ", &a[i]);
        merge(1, n);
//        for(int i = 1; i <= n; i++)
//            printf("%d ", a[i]);
        printf("%lld\n",ans);
    }
    return 0;
}



第二种:
树状数组,参考大牛的 http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html

代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;
const int maxn = 500005;

int n;
int a[maxn], b[maxn];
struct node
{
    int val, id;
}in[maxn];
int lowbit(int t)
{
    return t & (-t);
}
void add(int t, int d)
{
    for(int i = t; i <= n; i += lowbit(i))
        a[i] += d;
}
int get_sum(int t)
{
    int s = 0;
    for(int i = t; i >= 1; i -= lowbit(i))
        s += a[i];
    return s;
}
int cmp(node a, node b)
{
    return a.val < b.val;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%d ", &n) && n)
    {
        memset(b, 0, sizeof(b));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d ", &in[i].val);
            in[i].id = i;
        }
        sort (in + 1, in + 1 + n, cmp);
        for(int i = 1; i <= n; i++)
            b[in[i].id] = i;
        __int64 ans = 0;
        memset(a, 0, sizeof(a));
        for(int i = 1; i <= n; i++)
        {
            add(b[i], 1);
            ans += i - get_sum(b[i]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值