http://acm.hdu.edu.cn/showproblem.php?pid=1394

Minimum Inversion Number

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

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
#include<stdio.h>
#define maxn 500004
struct node
{
int left,right;
int sum;
};
int val[maxn];
int n;
node tree[*maxn];
void build(int left,int right,int i)
{
tree[i].left =left;
tree[i].right =right;
tree[i].sum =;
if(tree[i].left ==tree[i].right )
return ;
build(left,(left+right)/,*i);
build((left+right)/+,right,*i+);
}
void update(int r,int j)
{
tree[r].sum++;
if(tree[r].left==tree[r].right)
return ;
int mid=(tree[r].left +tree[r].right )/;
if(j<=mid)
update(r*,j);
if(j>mid)
update(r*+,j);
}
int getsum(int p, int left, int right)
{
if (left > right)
return ;
if (tree[p].left == left && tree[p].right == right)
return tree[p].sum;
int m = (tree[p].left + tree[p].right) / ;
if (right <= m)
return getsum(p*, left, right);
else if (left > m)
return getsum(p*+, left, right);
else return getsum(p*, left, m) + getsum(p*+, m + , right);
}
int main()
{
while (~scanf("%d",&n))
{
build(,n - ,);
int i,sum =,ans;
for (i = ;i<= n; i++)
{
scanf("%d", &val[i]);
sum += getsum(, val[i], n - );
update(, val[i]);
}
ans = sum;
for (i = ; i <= n; i++)
{
sum = sum + (n - val[i] - ) - val[i];
ans = sum < ans ? sum : ans;
}
printf("%d\n", ans);
}
return ;
}
 

HDU1394-Minimum Inversion Number的更多相关文章

  1. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  2. [hdu1394]Minimum Inversion Number(树状数组)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  4. HDU1394 Minimum Inversion Number(线段树OR归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. HDU-1394 Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  6. 2018.07.08 hdu1394 Minimum Inversion Number(线段树)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  7. hdu1394 Minimum Inversion Number(最小逆序数)

    Minimum Inversion Number Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/O ...

  8. HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)

    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...

  9. hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)

    题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  10. 【线段树】HDU1394 - Minimum Inversion Number

    [题目大意] 给出0..n-1组成的一段数,可以移动前几个数到结尾.求出最小的逆序对个数. [思路] 先用线段树求出逆序对,方法和树状数组是一样的.然后对于当前第一个数num[0],在它之后比它小的数 ...

随机推荐

  1. ReactNative-----环境搭建(android)

    1.参考文档 http://reactnative.cn/docs/0.26/getting-started.html http://reactnative.cn/docs/0.26/running- ...

  2. 桶排序之python实现源码

    tmp = [] def bucket_sort(old): for i in range(len(old)): tmp.append([]) for i in old: tmp[int( i * l ...

  3. NODE JS拼命吹,我就不喜欢. 别问为什么,直觉.

    NODE JS拼命吹,我就不喜欢. 别问为什么,直觉. 来看看node js 在paypal的捣鼓文章吧.https://www.paypal-engineering.com/2013/11/22/n ...

  4. ASP.NET中的验证控件

    ASP.NET提供了如下的控件: RequiredFieldValidator: 字段必填 (ControlTovalidate设定要验证的控件) RangeValidator: 值在给定的最大值,最 ...

  5. Docker命令使用详解

    其中<>括起来的参数为必选, []括起来为可选 docker -exec -i -t 3f407013d8c0 /bin/bash    进入容器 docker version查看dock ...

  6. shell sort

    sort is awesome! 22 hpp_national:talk:recentAtUsers-64490 23 hpp_national:talk:site_online_users:178 ...

  7. php MVC 及例子解释

    根据http://www.21ds.net/article/4/453改写: MVC模式在网站架构中十分常见.它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以 ...

  8. javascript 事件流及应用

    当页面元素触发事件的时候,该元素的容器以及整个页面都会按照特定顺序发生该元素的触发 事件,事件传播的顺序叫做事件流 1.事件流的分类: A.冒泡型事件(所有浏览器都支持)   由明确的事件源到最不确定 ...

  9. angularjs制作的iframe后台管理页切换页面

    <code> <!DOCTYPE html><html lang="zh" ng-app><head> <meta chars ...

  10. linux下安装phpredis

    一.redis安装 1. yum install redis (不行的先执行:yum install epel-release,再yum install redis) 2.启动服务 redis-ser ...