Minimum Inversion Number HDU - 1394

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.
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

题意:给你一串由0-n-1组成的序列,每次将头元素移动至序列尾部,对每个变换求一次逆序对,问在这些变换的序列中序列逆序对最小是多少。
思路:首先求逆序对,线段树区间 (l,r)的和 ,这个和表示序列到目前为止l,r中出现数字的次数,然后当前位置数x的逆序对就是(x+1,n)的结果,由于根据题意序列是0-n-1,元素不重复,所以避免越界可以query(x,n)。。。我建树从1建到n而且会有越界风险感觉很low2333来神的线段树好像就,神TM高端2333
然后把第一个初始序列的逆序对处理出来之后,由于这个题意这个操作啊,就很神奇 ,毛估估是有规律的对不对。看 序列 2 4 1 3 我们操作要移动2。这个2在队首对这个序列提供的逆序对贡献是比这个数小的所有数的个数,2移到队尾对逆序对的贡献是所有比2大的数字个数之和。那么我们对上一次的总数减去比当前数小的个数 加上比当前数大的个数 取一个最小就可以了
恕我直言 大半夜脑子不大好使。。我printf里面一开始写了个tot在那里狂debug。。。一度绝望。。

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
using namespace std;

//thanks to pyf ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int,int> PII;
typedef long long ll;

const int N = 1e5+5;

struct Tree
{
    int l,r,sum;
}t[N*4];
int a[N];
void push_up(int step)
{
    t[step].sum = t[step*2].sum + t[step*2+1].sum;
}
void build(int l,int r,int step)
{
    t[step].l = l;
    t[step].r = r;
    t[step].sum = 0;
    if(l==r)
    {
        t[step].sum = 0;
        return ;
    }
    int mid = (l+r)/2;
    build(l,mid,step*2);
    build(mid+1,r,step*2+1);
}
void update(int x,int step)
{
    if(t[step].l==t[step].r)
    {
        t[step].sum ++;
        return;
    }
    int mid = (t[step].l + t[step].r)/2;
    if(x<=mid)
        update(x,step*2);
    else
        update(x,step*2+1);
    push_up(step);
}
int query(int l,int r,int step)
{
//  cout << l << " " << r << " " << t[step].l << " " << t[step].r << endl;
    if(t[step].l == l&&t[step].r == r)
        return t[step].sum;
    int mid = (t[step].l + t[step].r)/2;
    if(r<=mid)
        return query(l,r,step*2);
    else if(l>mid)
        return query(l,r,step*2+1);
    else
        return query(l,mid,step*2) + query(mid+1,r,step*2+1);
}
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            a[i] = x+1;
        }
        build(1,n,1);
        int tot = 0;
        for(int i=0;i<n;i++)
        {
//          cout << a[i] << endl;
            tot += query(a[i],n,1);
            update(a[i],1);         
        }
        int ans = tot;
        for(int i=0;i<n;i++)
        {
            tot += n-a[i];
            tot -= a[i] -1;
//          cout << ans << " "<< tot << endl;
            ans = min(ans,tot);
        }
        printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值