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. javascript--setTimeout定时器

    setTimeout()  可以理解为 定时炸弹      ---------------->隔一段事件执行,并且只会执行一次 函数语法: setTimeout(参数1,参数2) 参数1:待执行 ...

  2. jQuery-qrcode.js 生成带Logo 的二维码

    引入文件  jQuery-qrcode.js 地址:https://blog-static.cnblogs.com/files/kitty-blog/jquery-qrcode.js https:// ...

  3. Ajax在表单中的应用

    ajax在注册用户表单中的使用 1.验证用户名是否被使用 2.获取手机短信验证码 3.点击表单中的图片刷新,可实现刷新图片验证码 <!DOCTYPE html> <html> ...

  4. 【shell脚本学习-2】

    #!/bin/bash - #echo do you have exetuate this project \n printf "please input your passwd" ...

  5. thinkphp模板如何转换时间格式?

    <!-- 如果有日期输出,即$data.time不为空且不为0,则格式化时间戳,否则默认当前时间戳,并格式化成日期格式 --> {$data.time|default=time()|dat ...

  6. STM32F4使用FPU+DSP库进行FFT运算的测试过程二

    原文地址:http://www.cnblogs.com/NickQ/p/8541156.html 测试环境:单片机:STM32F407ZGT6 IDE:Keil5.20.0.0 固件库版本:STM32 ...

  7. Redis缓存数据库的安装与配置(1)

    1.安装 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目录下这些文件作用如下 redis ...

  8. 一起来学习Shell脚本

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

  9. PRO*C 函数事例 1 -- 数据库连接、事务处理

    1.程序结构        每一个Pro*C程序都包括两部分:(1)应用程序首部:(2)应用程序体        应用程序首部定义了ORACLE数据库的有关变量, 为在C语言中操纵ORACLE数据库做 ...

  10. Win10开发笔记(一):一些VS2015中可能遇到的问题

    Win10开发者交流群:53078485 一.VS2015部署Win10程序到手机出现“0x80073CFD”错误解决方案 在VS2015 RC中创建了Windows Universal程序,部署到手 ...