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
先求出逆序数,在一个个dp
 #include"iostream"
#include"cstdio"
#include"string"
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MAX 5004
int s[MAX];
int sum;
struct node
{
int l;
int r;
int va;
}tree[MAX<<];
void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].va=;
if(tree[rt].l==tree[rt].r)
return ;
int mid=(tree[rt].l+tree[rt].r)>>;
build(lson);
build(rson);
}
void updata(int x,int rt)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].va=;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(x<=mid)
updata(x,rt<<);
else
updata(x,rt<<|);
tree[rt].va=tree[rt<<].va+tree[rt<<|].va;
}
void query(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
sum+=tree[rt].va;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(r<mid)
query(l,r,rt<<);
else if(l>mid)
query(l,r,rt<<|);
else
{
query(lson);
query(rson);
}
}
int main()
{
int n,i,t,m;
while(scanf("%d",&n)!=EOF)
{
build(,n-,);
sum=;
for(i=;i<n;i++)
{
scanf("%d",&s[i]);
query(s[i],n-,);
updata(s[i],);
}
m=sum;
for(i=;i<n;i++)
{
sum=sum-s[i]+n--s[i];
if(m>sum)
m=sum;
}
printf("%d\n",m);
}
return ;
}

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

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  3. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  4. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  5. 【hdu1394】Minimum Inversion Number

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

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

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

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

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

  8. Minimum Inversion Number

    Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  9. 逆序数2 HDOJ 1394 Minimum Inversion Number

    题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...

  10. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

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

随机推荐

  1. bzoj 1419 Red is good(期望DP)

    [题意] R红B蓝,选红得1选蓝失1,问最优状态下的期望得分. [思路] 设f[i][j]为i个Rj个B时的最优期望得分,则有转移式为: f[i][j]=max{ 0,(f[i-1][j]+1)*(i ...

  2. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  3. 使用MATLAB生成模糊控制的离线查询表

    1.打开模糊控制工具箱,编辑输入输出变量的隶属度函数和模糊控制规则,如下图所示,导出为fuzzy_control.fis文件. 2.打开Simulink模块,建立下图所示的系统框图,两输入,一输出,处 ...

  4. A题进行时--浙大PAT 1011-1020

    #include<stdio.h> #include<string.h> int main(){ ]; ]; ]; ]; ]; int i; float sum; memset ...

  5. Chapter 2 Build Caffe

    Caffe for windows 的build药按照一定的顺序进行. ============================================================ 先以b ...

  6. struts2+Hibernate4+spring3+EasyUI环境搭建之五:引入jquery easyui

    1.下载jquery easyui组件     http://www.jeasyui.com/download/index.php 2.解压 放到工程中  如图 3.jsp引入组件:必须按照如下顺序 ...

  7. App Extension编程指南(iOS8/OS X v10.10)中文版

    http://www.cocoachina.com/ios/20141023/10027.html 当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思 ...

  8. JS代码格式化修改表格的数值的格式

    今天在cognos中第一次需要用到JS,主要是报表页面展示的时候是可能得到如下的数据 ,我需要对其中类型中有金额字样的,后面的数值,精确2位小数:有百分比字样的,数值显示成百分比.如下. 我先尝试了自 ...

  9. Gym 100507I Traffic Jam in Flower Town (模拟)

    Traffic Jam in Flower Town 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/I Description ...

  10. Android流量监控 思路,想法

    1,开启一个服务,每5分钟跑动一次更新流量,用于能够准确记录流量         每一个小时,更新一次流量,用于清除非本月的流量 2,保存流量的时候,进行判断         a,若是数据库中保存的 ...