OpenJ_Bailian - 2299 - Ultra-QuickSort (树状数组 & 线段树 & 求逆序数)

OpenJ_Bailian - 2299 - Ultra-QuickSort

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
题目链接

这个题目就是一个求逆序,大多数的求逆序都是会用到树状数组,因为树状数组就是求一个前缀和,简单的几行代码就可以了,套一个模板,然后就是你在输入过程中查询就好了,看一下他前面出现了几个比他小的,再用应该有几个比他小的减去这个数,就说明剩下应该出现的比他小的这些数都跑到了他后面,不就是逆序了么。这里挂两种解题方法,树状数组和线段树,我个人还是比较喜欢线段树,虽然树状数组代码简单,但是线段树真的很强,树状数组能解决的,线段树基本也可以,就算数据量很大,离散化就好了,而且也很高效,这个题目的线段树解题就需要离散化。
树状数组AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int maxx=1001000;
int n;
ll c[maxx];

ll lowbit(ll x)
{
    return x & -x;
}

void update(ll pos, ll val)
{
    while(pos < maxx)
    {
        c[pos] += val;
        pos += lowbit(pos);
    }
}

ll query(ll pos)
{
    ll sum = 0;
    while(pos > 0)
    {
        sum += c[pos];
        pos -= lowbit(pos);
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n), n)
    {
        int temp;
        ll ans = 0;
        memset(c, 0, sizeof(c));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            int cnt = query(temp + 1);
            ans += i - cnt;
            update(temp + 1, 1);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

线段树的话一定注意数组别开小了,因为这个题目的数据量比较大,然后就是记得要去重,还有就是判断一下你的边界。这个就是求你当前点的位置之后的和,也就是看比他大并且先出现的有多少个数,这里离散化一下,然后查询的时候,一定注意pos + 1 <= len,因为poj上数据有点水,所以不判断也能过,但是学长嘱咐了这个地方要判断一下,我也在这里做一下笔记,多留意一下
线段树AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 2e6 + 5;
int a[maxn], b[maxn];

struct node
{
    ll l, r;
    ll val;
}p[maxn];

void build(int l, int r, int cur)
{
    p[cur].l = l, p[cur].r = r;
    p[cur].val = 0;
    if(l == r)  return ;
    int m = (l + r) >> 1;
    build(l, m, cur << 1);
    build(m + 1, r, cur << 1 | 1);
}

void pushup(int cur)
{
    p[cur].val = p[cur << 1].val + p[cur << 1 | 1].val;
}

void update(int pos, int val, int cur)
{
    int l = p[cur].l, r = p[cur].r;
    if(l == r)
    {
        p[cur].val += val;
        return ;
    }
    int m = (l + r) >> 1;
    if(pos <= m)    update(pos, val, cur << 1);
    else    update(pos, val, cur << 1 | 1);
    pushup(cur);
}

ll query(int ql, int qr, int cur)
{
    ll l = p[cur].l, r = p[cur].r;
    ll ans = 0;
    if(ql <= l && r <= qr)  return p[cur].val;
    int m = (l + r) >> 1;
    if(ql <= m) ans += query(ql, qr, cur << 1);
    if(qr > m)  ans += query(ql, qr, cur << 1 | 1);
    return ans;

}

int main()
{
    int n;
    while(~scanf("%d",&n), n)
    {
        int temp;
        ll ans = 0, cnt = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            a[++cnt] = temp;
            b[cnt] = temp;
        }
        sort(b + 1, b + 1 + cnt);
        int len = unique(b + 1, b + 1 + cnt) - b - 1;
        build(1, len, 1);
        for(int i = 1; i <= cnt; i++)
        {
            int pos = lower_bound(b + 1, b + 1 + cnt, a[i]) - b;
            ll res;
            if(pos + 1 <= len) res = query(pos+1,len, 1);
            else res=0;
            ans += res;
            update(pos, 1, 1);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/81558600
今日推荐