poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299

最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧

1、初始化as[i]=i;对as数组按照num[]的大小间接排序

2、bs[as[i]]=i;现在bs数组就是num[]数组的离散化后的结果

3、注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环...

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)

const int MAXN = 500000 +100;
int n;

int C[MAXN],num[MAXN],as[MAXN],bs[MAXN],x[MAXN];

inline int lowbit(int i){return i&(-i);}
void add(int i,int v)
{
    for(;i<n;C[i]+=v,i+=lowbit(i));//printf("#i=%d#\n",i);
}
int sum(int i)
{
    int s=0;
    for(;i>0;s+=C[i],i-=lowbit(i));
    return s;
}

bool cmp(const int a,const int b)
{
    return num[a]<num[b];
}

int main()
{
    //IN("poj2299.txt");
    ll ans;
    while(~scanf("%d",&n) && n)
    {
        ans=0;CL(C,0);CL(x,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            as[i]=i;
        }
        sort(as+1,as+1+n,cmp);
        for(int i=1;i<=n;i++)
        {
            bs[as[i]]=i;
        }
        /////////////////////
        //for(int i=1;i<=n;i++)
          //  printf("i=%d bs=%d\n",i,bs[i]);
        ////////////////////////
        for(int i=1;i<=n;i++)
        {
            add(bs[i],1);
            x[i]=sum(bs[i]-1);
        }
        for(int i=1;i<=n;i++)
        {
            ans+=sum(bs[i]-1)-x[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

poj 2299 树状数组求逆序数+离散化

时间: 2024-12-09 17:50:56

poj 2299 树状数组求逆序数+离散化的相关文章

ZOJ-2386 Ultra-QuickSort 【树状数组求逆序数+离散化】

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 seque

poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

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

POJ 2299 -Ultra-QuickSort-树状数组求逆序数

POJ 2299Ultra-QuickSort 使用树状数组记录逆序对数. 把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 5 using namespace std; 6 7 const int maxn = 500005; 8 int reflect[maxn],c[maxn],N; 9 10 struct Node

POJ2299 Ultra-QuickSort(树状数组求逆序数+离散化)

原文:http://blog.csdn.net/alongela/article/details/8142965 给定n个数,要求这些数构成的逆序对的个数.除了用归并排序来求逆序对个数,还可以使用树状数组来求解. 树状数组求解的思路:开一个能大小为这些数的最大值的树状数组,并全部置0.从头到尾读入这些数,每读入一个数就更新树状数组,查看它前面比它小的 已出现过的有多少个数sum,然后用当前位置减去该sum,就可以得到当前数导致的逆序对数了.把所有的加起来就是总的逆序对数. 题目中的数都是独一无二

树状数组求逆序数

poj 2299 树状数组求逆序数题目链接:http://poj.org/problem?id=2299 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

poj2299 Ultra-QuickSort 树状数组求逆序数

poj2299 Ultra-QuickSort   树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 49587   Accepted: 18153 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequenc