逆序数&&线段树
Minimum Inversion Number hdu 1394
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
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)
You are asked to write a program to find the minimum inversion number out of the above sequences.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
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)
You are asked to write a program to find the minimum inversion number out of the above sequences.
InputThe input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
OutputFor each case, output the minimum inversion number on a single line.
Sample Input
10 1 3 6 9 0 8 5 7 4 2
Sample Output
16
每次可以将第一个数放在最后构成一个新的序列,然后求逆序数最少的序列。
1 3 6 9 0 8 5 7 4 2 变成 3 6 9 0 8 5 7 4 2 1 我们发现逆序数减少了3个(0,1,2)但增加了10-3-1=6个(6,9,8,5,7,4)也就是 n-a[i]-1个
我们使用线段树的思想,每次我们插入判断 a[i]+1~n 也就是比a[i]大的数是否出现过,出现过那个就是一个逆序对。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<cmath> #define maxn 5100 #define ll long long int #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int a[maxn],sum[maxn<<2]; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(int l,int r,int rt) { sum[rt]=0; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson); } void updata(int L,int l,int r,int rt) { if(l==r) { sum[rt]++; return ; } int m=(l+r)>>1; if(L<=m) updata(L,lson); else updata(L,rson); pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&R>=r) return sum[rt]; int m=(l+r)>>1; int cnt=0; if(L<=m) cnt+=query(L,R,lson); if(R>m) cnt+=query(L,R,rson); return cnt; } int main() { int n; while(~scanf("%d",&n)) { int ans=0; build(1,n,1); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); ans+=query(a[i]+1,n,1,n,1); updata(a[i]+1,1,n,1); } int Min=ans; for(int i=1; i<=n; i++) { ans+=n-a[i]*2-1; Min=min(ans,Min); } printf("%d\n",Min); } }
Ultra-QuickSort
Description
![](http://poj.org/images/2299_1.jpg)
Ultra-QuickSort produces the output
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
逆序对,数据太大,我们先离散化,然后直接跑就ok了 因为没有重复元素就不用unique了
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<cmath> #define maxn 500010 #define ll long long int #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int b[maxn],tree[maxn]; int n; struct node { int x,id; bool operator < (const node &u) const { return x<u.x; } } a[maxn]; void add(int k,int num) { while(k<=n) { tree[k]+=num; k+=k&(-k); } } int sum(int k) { int ans=0; while(k) { ans+=tree[k]; k-=k&(-k); } return ans; } int main() { while(scanf("%d",&n),n) { for(int i=1; i<=n; i++) { scanf("%d",&a[i].x); a[i].id=i; tree[i]=0; } sort(a+1,a+n+1); for(int i=1; i<=n; i++) b[a[i].id]=i; ll cnt=0; for(int i=1; i<=n; i++) { add(b[i],1); cnt+=i-sum(b[i]); } printf("%lld\n",cnt); } }
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~