标签:algo return space cstring while stream blog sum nbsp
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct segtree { int left,right,sum; }tree[10050]; int n,a[5050]; void build(int pos,int l,int r) { tree[pos].left = l; tree[pos].right = r; tree[pos].sum = 0; if(l < r) { int mid = (l+r)/2; build(pos*2,l,mid); build(pos*2+1,mid+1,r); } } int getsum(int pos,int l,int r) { if(tree[pos].left == l && tree[pos].right == r) return tree[pos].sum; int mid = (tree[pos].left+tree[pos].right)/2; if(r <= mid) return getsum(pos*2,l,r); if(l > mid) return getsum(pos*2+1,l,r); return getsum(pos*2,l,mid)+getsum(pos*2+1,mid+1,r); } void update(int pos,int num) { tree[pos].sum++; if(tree[pos].left == tree[pos].right) return; int mid = (tree[pos].left+tree[pos].right)/2; if(num <= mid) update(pos*2,num); else update(pos*2+1,num); } int main() { while(~scanf("%d",&n)) { int ans = 0; build(1,0,n-1); for(int i = 0;i < n;i++) { scanf("%d",&a[i]); ans += getsum(1,a[i],n-1); update(1,a[i]); } int temp = ans; for(int i = 0;i < n-1;i++) { temp += n-1-a[i]-a[i]; ans = min(temp,ans); } printf("%d\n",ans); } return 0; } //HDU1394
标签:algo return space cstring while stream blog sum nbsp
原文地址:http://www.cnblogs.com/zhurb/p/6060592.html