HDU 1394 Minimum Inversion Number [线段树->单点更新]【数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 ————————————————–. Minimum Inversion Number Problem Description For a given sequence of numbers a1,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,an-1,an (where m = 0 - the initial seqence) You are asked to write a program to find the minimum inversion number out of the above sequences. Input Output Sample Input Sample Output Author Source —————————————————. 题目大意: 解题思路: 在每次把数据挂到树上之前 可以区间查询的方式计算这个值的逆序数 然后O(
然后他要求的序列一共有n个那样的话不能O(
然后想最后发现了有一个规律; 附本题代码 #include <bits/stdc++.h>
#define abs(x) (((x)>0)?(x):-(x))
#define lalal puts("*********")
#define Rep(a,b,c) for(int a=(b);a<=(c);a++)
#define Req(a,c) for(int a=(b);a>=(c);a--)
#define Rop(a,c) for(int a=(b);a<(c);a++)
#define s1(a) scanf("%d",&a)
typedef long long int LL;
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 9901;
/**************************************/
const int N = 10000+5;
struct node
{
int l,r;
int val;
int md()
{
return (l+r)>>1;
}
}tree[N<<2];
int a[N],ans;
#define ll (rt<<1)
#define rr (rt<<1|1)
#define mid (tree[rt].md())
void pushup(int rt)
{
tree[rt].val=tree[ll].val+tree[rr].val;
}
void build(int rt,int l,int r)
{
tree[rt].l=l,tree[rt].r=r;
if(l==r)
{
tree[rt].val=0;
return ;
}
build(ll,l,mid);
build(rr,mid+1,r);
pushup(rt);
return;
}
void update(int rt,int pos,int val)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].val+=val;
return;
}
if(pos<=mid)update(ll,pos,val);
else update(rr,val);
pushup(rt);
return;
}
void query(int rt,int L,int R)
{
if(L<=tree[rt].l&&tree[rt].r<=R)
{
ans += tree[rt].val;
return;
}
if(R<=mid)
query(ll,L,R);
else if(L>mid)
query(rr,R);
else
{
query(ll,R);
query(rr,R);
}
return;
}
int main()
{
int n;
while(~s1(n))
{
build(1,0,n);
int sum = 0;
Rep(i,1,n)
{
s1(a[i]);
ans = 0;
query(1,a[i]+1,n);
sum+=ans;
update(1,a[i],1);
}
int mi = sum;
Rep(i,n)
{
sum+=n-a[i]-a[i]-1;
if(sum<mi)
mi=sum;
}
printf("%dn",mi);
}
return 0;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |