首页 > 代码库 > 归并求逆序数 && 线段树求逆序数
归并求逆序数 && 线段树求逆序数
Brainman
Time Limit: 1000 MS Memory Limit: 30000 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status] [Discuss]
Description
Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.
Problem Here‘s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:Start with: 2 8 0 3 swap (2 8) 8 2 0 3 swap (2 0) 8 0 2 3 swap (2 3) 8 0 3 2 swap (8 0) 0 8 3 2 swap (8 3) 0 3 8 2 swap (8 2) 0 3 2 8 swap (3 2) 0 2 3 8 swap (3 8) 0 2 8 3 swap (8 3) 0 2 3 8 So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:Start with: 2 8 0 3 swap (8 0) 2 0 8 3 swap (2 0) 0 2 8 3 swap (8 3) 0 2 3 8 The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond‘s mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.
Problem Here‘s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Input
The first line contains the number of scenarios. For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.
Sample Input
44 2 8 0 310 0 1 2 3 4 5 6 7 8 96 -42 23 6 28 -100 655375 0 0 0 0 0
Sample Output
Scenario #1:3Scenario #2:0Scenario #3:5Scenario #4:0
#include<iostream>#include<cstdio>#include<stdlib.h>#define N 1000010using namespace std;long long ans;int a[N];void merge(int s1,int e1,int s2,int e2){ int p1,p2,p; int* temp = new int[e2-s1+5]; p=0;p1=s1;p2=s2; while(p1<=e1&&p2<=e2) { if(a[p1]<=a[p2]) { temp[p++]=a[p1++]; continue; } else { temp[p++]=a[p2++]; ans+=e1-p1+1; //关键所在 continue; } } while(p1<=e1) temp[p++]=a[p1++]; while(p2<=e2) temp[p++]=a[p2++]; int i; for(i=s1;i<=e2;i++) a[i]=temp[i-s1]; delete temp;}void merge_sort(int s,int e){ int m; if(s<e) { m=(s+e)/2; merge_sort(s,m); merge_sort(m+1,e); merge(s,m,m+1,e); }}int main(){ int test; int num; scanf("%d",&test); for(num=1;num<=test;num++) { int n,i; ans=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); merge_sort(0,n-1); printf("Scenario #%d:\n",num); printf("%lld\n",ans); if(num!=test) printf("\n"); }}
#include<iostream>#include<cstring>#include <stdio.h>#include<algorithm>using namespace std;int n;int static c[500003];int a[500003];const int maxn = 500003;struct Node{ int value,pos; bool operator<(const Node a)const { return value<a.value; }} s[500003];int lowbit(int x){ return x&(-x);}void insert(int pos,int x){ while(pos<=maxn) { c[pos]+=x; pos+=lowbit(pos); }}int sum(int x){ int s=0; while(x>0) { s+=c[x]; x-=lowbit(x); } return s;}int main(){ int t,num; cin>>t; for(num=1; num<=t; num++) { scanf("%d",&n); memset(c,0,sizeof(c)); memset(a,0,sizeof(a)); for(int i=0; i<n; i++) { scanf("%d",&s[i].value); s[i].pos=i+1; } sort(s,s+n); int id=1; a[s[0].pos]=1; for(int i=1; i<n; i++) //离散化 { if(s[i].value!=s[i-1].value) { a[s[i].pos]=++id; } else { a[s[i].pos]=id; } } long long int ans=0; for(int i=1; i<=n; i++) //一个个插入求逆序对 { insert(a[i],1); ans+=(i-sum(a[i])); } printf("Scenario #%d:\n",num); printf("%lld\n",ans); if(num!=t) printf("\n"); } return 0;}
树状数组时间空间上都没有归并好~~~~~~~
归并求逆序数 && 线段树求逆序数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。