本文共 2373 字,大约阅读时间需要 7 分钟。
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18319 Accepted Submission(s): 11123 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.
The 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.
For each case, output the minimum inversion number on a single line.
Ignatius.L | We have carefully selected several similar problems for you:
#include #include #include #include using namespace std;int a[5500];int n;int main(){ while(~scanf("%d",&n)) //总共最多有n-1种逆数对 { for(int i=0;i a[j]) ans++; } } int res=999999999; if(ans
线段树解法
#include #include #include #include using namespace std; #define N 5555#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[N<<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); pushup(rt);}void update(int p,int l,int r,int rt){ if(l==r) { sum[rt]++;//由于存入了p这个点 sum[rt]的数量要加一 在递归更新节点的sum值 return ; } int m=(l+r)>>1; if(p<=m)update(p,lson); else update(p,rson); pushup(rt);}int query(int L,int R,int l,int r,int rt)//将L到R之间有多少点存在的数量 求出来 { if(L<=l&&R>=r) { return sum[rt]; } int m=(l+r)>>1; int res=0; if(L<=m)res+=query(L,R,lson); if(R>m)res+=query(L,R,rson); return res;}int x[N];int main(){ int n; while(~scanf("%d",&n)) { build(0,n-1,1); int sum=0; for(int i=0;i
转载地址:http://dafci.baihongyu.com/