BIT+离散化
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,a[500010]; 6 int num[500010]; 7 long long ans; 8 struct node{ 9 int id,w; 10 }q[500010]; 11 int cmp(node a,node b) 12 { 13 return a.w<b.w; 14 } 15 int lowbit(int i){ 16 return i&(-i); 17 } 18 int sum(int i){ 19 int cnt=0; 20 while(i>=1){ 21 cnt+=num[i];i-=lowbit(i); 22 } 23 return cnt; 24 } 25 void update(int i,int val){ 26 while(i<=n){ 27 num[i]+=val;i+=lowbit(i); 28 } 29 } 30 int main() 31 { 32 int i,j,k; 33 while(scanf("%d",&n)!=EOF){ 34 if(n==0) break; 35 memset(num,0,sizeof(num)); 36 ans=0; 37 for(i=1;i<=n;i++){ 38 q[i].id=i; 39 scanf("%d",&q[i].w); 40 } 41 sort(q+1,q+n+1,cmp); 42 for(i=1;i<=n;i++) a[q[i].id]=i; 43 for(j=1;j<=n;j++) 44 { 45 update(a[j],1); 46 ans+=j-sum(a[j]); 47 } 48 printf("%lld ",ans); 49 } 50 return 0; 51 }
1 #include <vector> 2 #include <map> 3 #include <set> 4 #include <algorithm> 5 #include <iostream> 6 #include <cstdio> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include <cstring> 11 #include <queue> 12 using namespace std; 13 #define INF 0x3f3f3f3f 14 #define MAX 100010 15 16 typedef long long ll; 17 int n,a[MAX],bit[MAX]; 18 19 int sum(int i) 20 { 21 int s=0; 22 while(i>0){ 23 s+=bit[i]; 24 i -= i&-i; 25 } 26 return s; 27 } 28 29 void add(int i,int x) 30 { 31 while(i<=n){ 32 bit[i]+=x; 33 i += i&-i; 34 } 35 } 36 37 void solve() 38 { 39 int ans=0; 40 for(int j=1; j<=n; j++){ 41 add(a[j],1); 42 ans += j-sum(a[j]); 43 } 44 printf("%d ",ans); 45 } 46 47 int main() 48 { 49 int t; 50 scanf("%d",&t); 51 while(t--){ 52 memset(a,0,sizeof(a)); 53 memset(bit,0,sizeof(bit)); 54 scanf("%d",&n); 55 for(int i=1; i<=n; i++){ 56 scanf("%d",&a[i]); 57 } 58 solve(); 59 } 60 }
归并:
1 #include <cstdio> 2 3 const int N=500000+10; 4 5 int n,a[N],temp[N]; 6 long long ans; 7 8 void merge_sort(int l,int r) 9 { 10 if (r-l<=1) return; 11 int m=(l+r)>>1; 12 int p=l,q=m,i=l; 13 merge_sort(l,m); 14 merge_sort(m,r); 15 while (p<m || q<r) 16 { 17 if (q>=r || (p<m && a[p]<=a[q])) temp[i++]=a[p++]; 18 else {temp[i++]=a[q++];ans+=m-p;} 19 } 20 for (i=l;i<r;i++) a[i]=temp[i]; 21 } 22 int main() 23 { 24 while (scanf("%d",&n),n) 25 { 26 for (int i=0;i<n;i++) 27 scanf("%d",a+i); 28 29 ans=0; 30 merge_sort(0,n); 31 printf("%lld ",ans); 32 } 33 return 0; 34 }
1 #include <vector> 2 #include <map> 3 #include <set> 4 #include <algorithm> 5 #include <iostream> 6 #include <cstdio> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include <cstring> 11 #include <queue> 12 using namespace std; 13 #define INF 0x3f3f3f3f 14 #define MAX 100010 15 16 const int N=500000+10; 17 18 int n,a[N],temp[N]; 19 long long ans; 20 21 void merge_sort(int l,int r) 22 { 23 if (r-l<=1) return; 24 int m=(l+r)>>1; 25 int p=l,q=m,i=l; 26 merge_sort(l,m); 27 merge_sort(m,r); 28 while (p<m || q<r) 29 { 30 if (q>=r || (p<m && a[p]<=a[q])) temp[i++]=a[p++]; 31 else {temp[i++]=a[q++];ans+=m-p;} 32 } 33 for (i=l;i<r;i++) a[i]=temp[i]; 34 } 35 int main() 36 { 37 while (scanf("%d",&n),n) 38 { 39 for (int i=0;i<n;i++) 40 scanf("%d",a+i); 41 42 ans=0; 43 merge_sort(0,n); 44 for (int i=0;i<n;i++) 45 printf("%d ",a[i]); 46 printf(" "); 47 printf("%lld ",ans); 48 } 49 return 0; 50 }
这个博客 讲的很不错:http://blog.csdn.net/suwei19870312/article/details/5293694