#HDU 1394 Minimum Inversion Number (权值线段树求逆序数)

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27502    Accepted Submission(s): 16082


 

Problem Description

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

题目大意 : 输入一个N的序列, 每个数字不重复, 让你输出把第一个数第放到最后,此时的逆序数, 执行N次, 输出最小的逆序数

思路 :每个排列的逆序数对之和为每个数的逆序数之和,所以要从头开始求出每个数比他大的有多少, 并存下来, 更新该数已经出现, 最后从头遍历一次, 下标从0开始, 如果将第一个数 X 放到最后一个, 逆序数增加N - X, 减少  X - 1, 合起来就是

N - 2  X + 1

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct node
{
    int l, r, sum;
}t[MAXN * 4];
int p[MAXN], n, m;
void build(int rt, int l, int r) {
    t[rt].l = l, t[rt].r = r;
    t[rt].sum = 0;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
}
int Query(int rt, int l, int r) {
    if (t[rt].l >= l && t[rt].r <= r) return t[rt].sum;
    int mid = (t[rt].l + t[rt].r) >> 1, ui = 0, vi = 0;
    if (mid >= l) ui = Query(ls, l, r);
    if (mid < r) vi = Query(rs, l, r);
    return ui + vi;
}
void Update(int rt, int x) {
    if (t[rt].l == x && t[rt].r == x) {
        t[rt].sum = 1;
        return;
    }
    int mid = (t[rt].l + t[rt].r) >> 1;
    if (mid >= x) Update(ls, x);
    else Update(rs, x);
    t[rt].sum = t[ls].sum + t[rs].sum;
}

int main()
{
    while (~sc("%d", &n) && n) {
        build(1, 0, n - 1);
        int ans = 0;
        for (int i = 0; i < n; i++) {
            sc("%d", &p[i]);
            ans += Query(1, p[i] + 1, n - 1);
            Update(1, p[i]);
        }
        int min_ = INF;
        for (int i = 0; i < n; i++) {
            ans += n - 2 * p[i] - 1;
            Min(min_, ans);
        }
        cout << min_ << endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值