#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define PB(x)(scanf("%I64d",&x))
#define PRB(x)(printf("%I64d\n",(x)))
typedef __int64 LL;
#define N 5005
#define M 105
#define Mod 1000
#define Inf 0x7fffffff
#define L(x) (x<<1)
#define R(x) ((x<<1)|1)
#define For(i,n) for(int i=0;i<n;i++)
#define lowbit(x) (x) & (-x)
#define mid(x,y) ((x+y)>>1)
struct tree
{
int l,r;
int sum;
};
tree T[4*N];
int n,m;
int ar[N];
inline int maxx(int a,int b)
{
return a>b?a:b;
}
inline int minx(int a,int b)
{
return a>b?b:a;
}
void build(int l,int r,int k)
{
T[k].l = l;
T[k].r = r;
T[k].sum = 0;
if(l == r) return ;
int mi = mid(l,r);
build(l,mi,L(k));
build(mi+1,r,R(k));
}
void update(int v,int var,int k)
{
if(T[k].l == T[k].r )
{
T[k].sum = v;
return ;
}
int mi = mid(T[k].l,T[k].r);
if(var<=mi) update(v,var,L(k));
else update(v,var,R(k));
T[k].sum = T[L(k)].sum + T[R(k)].sum;
}
LL sum(int l,int r,int k)
{
if(T[k].l>=l && T[k].r<=r) return T[k].sum;
int mi = mid(T[k].l,T[k].r);
int ans = 0;
if(l<=mi) ans += sum(l,r,L(k));
if(r>mi) ans += sum(l,r,R(k));
return ans;
}
void init()
{
while(scanf("%d",&n)!=EOF)
{
build(0,n,1);
int ans = 0;
For(i,n)
{
PF(ar[i]);
ans += sum(ar[i],n,1);
update(1,ar[i],1);
}
int reans = ans;
For(i,n)
{
ans += n-1-ar[i]-ar[i];
reans = minx(reans,ans);
}
PR(reans);
}
return ;
}
int main()
{
init();
return 0;
}
hdu 1394
最新推荐文章于 2019-04-09 18:21:58 发布