hdu1394-Minimum Inversion Number(线段树)
2015-08-28 10:25
411 查看
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<cstdlib> #include<map> #include<queue> #include <deque> #include <list> #include <ctime> #include <stack> #include <vector> #include<set> #define Maxn 5555 #define MOD typedef long long ll; #define FOR(i,j,n) for(int i=j;i<=n;i++) #define DFR(i,j,k) for(int i=j;i>=k;--i) #define lowbit(a) a&-a #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); int sum[Maxn<<2],x[Maxn]; 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 update(int p,int l,int r,int rt) { if(l==r){sum[rt]++;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) { if(L<=l&&r<=R)return sum[rt]; int m=(l+r)>>1; int ret=0; if(L<=m)ret+=query(L,R,lson); if(R>m)ret+=query(L,R,rson); return ret; } using namespace std; int main() { int n; while(~scanf("%d",&n)) { build(0,n-1,1); int sum=0; FOR(i,0,n-1) { scanf("%d",&x[i]); sum+=query(x[i],n-1,0,n-1,1); update(x[i],0,n-1,1); } int ret=sum; FOR(i,0,n-1) { sum+=n-x[i]-x[i]-1; ret = min(ret,sum); } printf("%d\n",ret); } return 0; }
相关文章推荐
- 线段树题集
- hdu1754
- HDU1394
- 敌兵布阵 (1)
- I Hate It (1)
- LCIS (2)
- A Simple Problem with Integers (2)
- Mayor's posters (3)
- Buy Tickets (3)
- 线段树
- UVA - 12532 Interval Product
- POJ 3264 Balanced Lineup
- hdu 1542 求矩形并的面积
- 关于数据结构之线段树
- poj 3225 关于集合运算
- poj 2352
- hdu1166敌兵布阵(线段树点修改)
- POJ 2352 Stars 线段树 pascal
- hdu 1698 Just A Hook 线段树的一道题
- HDU 1754 I Hate It