Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21314 Accepted Submission(s): 12780
Problem Description
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.
Input
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.
Output
For 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
Author
CHEN, Gaoli
类似树状数组, 线段树 建立 ,把 数的叶子作为每个数对应的位置, 然后根节点表示数目, 最后枚举所有的点 求逆序数, (单节点增加 , 区间求和)
逆序数 表达为 n-x-x-1;
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define S1(n) scanf("%d",&n)
#define S2(n,m) scanf("%d%d",&n,&m)
#define Pr(n) printf("%d\n",n)
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
ll inv[maxn*2],fac[maxn];
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
void INV(){inv[1] = 1;for(int i = 2; i < maxn; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);}}
void Fac(){fac[0]=1;for(int i=1;i<=maxn;i++)fac[i]=(fac[i-1]*i)%MOD;}
ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
ll inv2(ll b){return qpow(b,MOD-2);}
ll cal(ll m,ll n){if(m<n)return 0;return (fac[m]*inv[fac[n]]%MOD)%MOD*inv[fac[m-n]]%MOD;}
ll cals(ll m,ll n){if(m<n)return 0;return (fac[m]*inv1(fac[n])%MOD)%MOD*inv1(fac[m-n])%MOD;}
int n;
struct SegTree{
int val;
int addmark;
}Seg[maxn];
void PushUP(int root)
{
Seg[root].val=Seg[root*2+1].val+Seg[root*2+2].val;
}
void build(int root,int begin,int end)
{
Seg[root].addmark=0;
Seg[root].val=0;
if(begin==end)
return ;
//scanf("%d",&Seg[root].val);
else
{
int mid=(begin+end)>>1;
build(root*2+1,begin,mid);
build(root*2+2,mid+1,end);
PushUP(root);
}
}
int query(int root,int begin,int end,int left,int right)
{
if(left<=begin&&right>=end)
return Seg[root].val;
int mid=(begin+end)>>1;
int ans=0;
if(left<=mid) ans+= query(root*2+1,begin,mid,left,right);
if(right>mid) ans+= query(root*2+2,mid+1,end,left,right);
//cout<<"ans="<<ans<<endl;
return ans;
}
void insert(int root,int l,int r,int idex,int add)
{
if(l==r)
{
Seg[root].val+=add;
return;
}
int mid=(l+r)>>1;
if(idex<=mid)
insert(root*2+1,l,mid,idex,add);
else
insert(root*2+2,mid+1,r,idex,add);
PushUP(root);
}
int x[maxn];
int main()
{
while(~S1(n))
{
mem(x,0);
build(1,0,n-1);
int sum=0;
for(int i=0;i<n;i++)
{
S1(x[i]);
sum+=query(1,0,n-1,x[i],n-1);
insert(1,0,n-1,x[i],1);
}
int ans=sum;
//cout<<"sum==="<<ans<<endl;
for(int i=0;i<n;i++)
{
sum+=n-x[i]-x[i]-1;
ans=min(ans,sum);
}
Pr(ans);
}
return 0;
}
123