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
 
Solution:
  题意就是给定$n$个数的序列($0$到$n-1$),求出序列的符合要求的排列中逆序对的个数最小值,(符合要求就是可以每次将序列第一位数移到序列最后一位或者不移)。
  思路就是先用树状数组求出原序列的逆序对个数$tot$,设首位数为$x$,则每次移动逆序对增加的个数为$x$后面比$x$大的数的个数(即$n-x$),每次减小的个数为$x$后面比$x$小的数的个数(即$x-1$)。
  那么移动前逆序对个数为$tot$,移动首位后逆序数$tot=tot-(x-1)+(n-x)$,于是枚举$n$次移动的情况(第$1$次表示不移,即原序列),更新最小值$ans$就行了。
  (坑点在于$HDU$上数据有多组,开始我还以为自己错了、查了半天~~手动滑稽~~)
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define il inline
#define ll long long
#define N 5005
using namespace std;
int n,a[N],c[N],ans=,tot;
il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=a*+x-,x=getchar();
return f?-a:a;
}
il void update(int x,int k){
while(x<=n){
c[x]+=k;
x+=x&-x;
}
}
il int sum(int x){
int ans=;
while(x){
ans+=c[x];
x-=x&-x;
}
return ans;
}
int main()
{
while(scanf("%d",&n)==){
memset(c,,sizeof(c));
ans=,tot=;
for(int i=;i<=n;i++)a[i]=gi()+;
for(int i=n;i>=;i--){
update(a[i],);
tot+=sum(a[i]-);
}
for(int i=;i<=n;i++)tot=tot-(a[i]-)+(n-a[i]),ans=min(ans,tot);
printf("%d\n",ans);
}
return ;
}

HDU——1394 Minimum Inversion Number的更多相关文章

  1. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

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

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

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

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

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

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

  5. hdu 1394 Minimum Inversion Number - 树状数组

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

  6. hdu 1394 Minimum Inversion Number 逆序数/树状数组

    Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

  7. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394  //hdu 题目   Problem Description The inversion number ...

  8. HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】

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

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

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

随机推荐

  1. hibernate笔记1

    1.  数据库中的表关系 一对一.一对多(多对一).多对一 2.  如何确立表中的表关系 一对多的关系如何实现:使用外键约束,一的方称为主表,多的方称为从表. 外键:从表中有一列,该列的取值除了nul ...

  2. Java处理中文乱码问题

    package servlet; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.ser ...

  3. ERROR: bootstrap checks failed

    错误描述:Linux默认配置的参数过小,需要自己设置 max file descriptors [4096] for elasticsearch process is too low, increas ...

  4. Scrapy之CrawlSpider

    问题:如果我们想要对某一个网站的全站数据进行爬取?解决方案: 1. 手动请求的发送 2. CrawlSpider(推荐) CrawlSpider概念:CrawlSpider其实就是Spider的一个子 ...

  5. Andrew Ng Machine Learning Coursera学习笔记

    课程记录笔记如下: 1.目前ML的应用 包括:数据挖掘database mining.邮件过滤email anti-spam.机器人autonomous robotics.计算生物学computati ...

  6. Linux中程序的编译和链接过程

    1.从源码到可执行程序的步骤:预编译.编译.链接.strip 预编译:预编译器执行.譬如C中的宏定义就是由预编译器处理,注释等也是由预编译器处理的. 编译: 编译器来执行.把源码.c .S编程机器码. ...

  7. mysql5.7.19安装报错 无法定位程序输入点

    https://blog.csdn.net/t876587201/article/details/79503688

  8. 一起来学习Shell脚本

    Shell脚本 Shell脚本(shell script),是一种为shell编写的脚本程序. 大家所说的shell通常都是指的shell脚本,但其实shell与shell脚本是两个不同的概念.由于习 ...

  9. java 第五章 方法定义及调用

    1.方法的定义 什么是方法 方法是完成某个功能的一组语句,通常将常用的功能写成一个方法 方法的定义 [访问控制符] [修饰符] 返回值类型 方法名( (参数类型 形式参数, ,参数类型 形式参数, , ...

  10. 区分Oracle的数据库,实例,服务名,SID

    文章摘自:http://www.zhetao.com/content240 感谢分享O(∩_∩)O~ 在实际的开发应用中,关于Oracle数据库,经常听见有人说建立一个数据库,建立一个Instance ...