Question link:
Http://poj.org/problem? Id = 2299
Analysis and Summary:
When we see the first response to this question, we think it is the reverse order number = |
First use merge sort to calculate the reverse order number AC, which should be the fastest way to calculate the reverse order number, 391 Ms
Then we use a tree array, because each element ranges from 0 to a [I] To 999,999,999, which is too large. So we use map for discretization and the result times out...
Later, we found that map is not needed. The reason why map is used is that the value of the element is too large, but the value of the element does not need to be mapped. We only need to map the subscript position of the element! A maximum of 50 million data records can be saved directly using arrays, so that the ing value can be obtained at O (1) time.
Finally, we used the line segment tree and used 922ms...
Code:
1. Tree array + discretization
#include<iostream>#include<cstdio>#include<cstring>#include<iostream>#include<map>#include<algorithm>using namespace std;const int MAXN = 500005;typedef long long int64;int n,arr[MAXN],rank[MAXN]; int64 c[MAXN], cnt,id;struct node{ int val, id; friend bool operator<(const node&a,const node&b){ return a.val < b.val; }}tmp[MAXN];inline int lowbit(int x) {return x&(-x);}int64 sum(int x){ int64 ret=0; while(x>0){ ret += c[x]; x -= lowbit(x); } return ret;}void add(int x){ while(x <= id){ ++c[x]; x += lowbit(x); }}int main(){ while(~scanf("%d",&n) && n){ memset(c, 0, sizeof(c)); for(int i=0; i<n; ++i){ scanf("%d",&arr[i]); tmp[i].val = arr[i]; tmp[i].id = i; } sort(tmp, tmp+n); id = 1; for(int i=0; i<n; ++i){ if(!i || tmp[i].val!=tmp[i-1].val) rank[tmp[i].id] = id++; else rank[tmp[i].id] = rank[tmp[i-1].id]; } int64 ans=0; for(int i=0; i<n; ++i){ int x = rank[i]; ans += sum(id)-sum(x); add(x); } printf("%lld\n",ans); cnt = 0; } return 0;}
2. Line Segment tree + discretization
#include<iostream>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define mid ((left+right)>>1)#define lson rt<<1, left, m#define rson rt<<1|1,m+1,rightusing namespace std;const int MAXN = 500010;typedef long long int64;int n,arr[MAXN],rank[MAXN]; int64 c[MAXN<<2];struct node{ int val, id; friend bool operator<(const node&a,const node&b){ return a.val < b.val; }}tmp[MAXN];void update(int rt,int left,int right,int data){ ++c[rt]; if(left==right) return; int m = mid; if(data <= m) update(lson,data); else update(rson,data);}int64 query(int rt,int left,int right,int l,int r){ if(left==l && right==r) return c[rt]; int m = mid; if(r <= m) return query(lson,l,r); else if(l > m) return query(rson,l,r); else return query(lson,l,mid)+query(rson,mid+1,r);}int main(){ while(~scanf("%d",&n) && n){ memset(c, 0, sizeof(c)); memset(rank, 0, sizeof(rank)); for(int i=0; i<n; ++i){ scanf("%d",&arr[i]); tmp[i].val = arr[i]; tmp[i].id = i; } sort(tmp, tmp+n); for(int i=0; i<n; ++i){ rank[tmp[i].id] = i+1; } int64 ans=0; for(int i=0; i<n; ++i){ int x = rank[i]; ans += query(1,1,n+1,x+1,n+1); update(1,1,n+1,x); } printf("%lld\n",ans); } return 0;}
3. sort by merge to calculate the number of reverse orders
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 500005; typedef long int64; int N, arr [maxn], TMP [maxn]; int64 CNT; void merge_sort (int * a, int X, int y, int * t) {If (Y-x> 1) {int M = (x + y)> 1; // divide int P = x, q = m, I = x; merge_sort (A, X, M, t ); merge_sort (a, m, Y, T); While (P <M | q <Y) {If (q> = Y | (P <M & A [p] <= A [Q]) T [I ++] = A [p ++]; else {T [I ++] = A [q ++]; CNT + = m-P ;}} for (I = x; I <Y; ++ I) A [I] = T [I] ;}} int main () {While (~ Scanf ("% d", & N) {for (INT I = 0; I <n; ++ I) {scanf ("% d ", & arr [I]); TMP [I] = arr [I];} CNT = 0; merge_sort (ARR, 0, N, TMP ); printf ("% LLD \ n", CNT);} return 0 ;}
-- The significance of life is to give it a meaningful person.
Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)