Minimum Inversion Number

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

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
 
Author
CHEN, Gaoli
 
Source
 题意:
给长度是n的序列,每次将第一个数放到最后,直到数列再次变成最初的数列,求过程中最小的逆序数。
代码:
//求出最初的逆序数sum,每次操作将a[i]放到最后时更新sum:
//加上a[i]向后的顺序数(比他大的数的个数),减去a[i]向后的
//逆序数(比她小的数的个数),加上a[i]向前的(他前面的数放到后面了)
//顺序数减去a[i]向前的逆序数。所以两遍树状数组就解决了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int n,c[maxn*+],a[maxn+],sh[maxn+],ni[maxn+];
int lowbit(int x) {return x&(-x);}
void add(int id)
{
while(id<=){
c[id]++;
id+=lowbit(id);
}
}
int query(int id)
{
int s=;
while(id>){
s+=c[id];
id-=lowbit(id);
}
return s;
}
int main()
{
int n;
while(scanf("%d",&n)==){
int sum=;
memset(c,,sizeof(c));
memset(sh,,sizeof(sh));
memset(ni,,sizeof(ni));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
a[i]++;
add(a[i]);
sh[i]=query(a[i]-);
ni[i]=i-sh[i]-;
sum+=ni[i];
}
//for(int i=1;i<=n;i++) cout<<sh[i]<<" "<<ni[i]<<endl;
memset(c,,sizeof(c));
for(int i=n;i>=;i--){
add(a[i]);
int tmp=query(a[i]-);
sh[i]+=tmp;
ni[i]+=n-i+-tmp-;
}
//for(int i=1;i<=n;i++) cout<<sh[i]<<" "<<ni[i]<<endl;
int ans=sum;
for(int i=;i<=n;i++){
sum=sum+ni[i]-sh[i];
ans=min(ans,sum);
}
printf("%d\n",ans);
}
return ;
}

HDU1394 逆序数的更多相关文章

  1. hdu1394逆序数(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意:逆序数:即假设在数组a中,假如i<j,但是a[i]>a[j]. 现在有一个 ...

  2. 线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

    为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...

  3. 【线段树求逆序数】【HDU1394】Minimum Inversion Number

    题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...

  4. hdu-1394(线段树&逆序数的性质和求法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意: 给出一个序列,一对逆序数就是满足i<j&&a[i]>a[ ...

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

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

  6. 线段树-最小逆序数hdu1394

    title: 线段树-最小逆序数 date: 2018-10-12 17:19:16 tags: acm 算法 刷题 categories: ACM-线段树 概述 这是一道简单的线段树的题,,,当然还 ...

  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 ...

随机推荐

  1. Python3 小工具-TCP半连接扫描

    from scapy.all import * import optparse import threading def scan(ip,port): pkt=IP(dst=ip)/TCP(dport ...

  2. c# html 导出word

    [CustomAuthorize]        public FileResult ExportQuestionCenterWord(SearchBaseQuestion search)       ...

  3. POJ 2069 Super Star(计算几何の最小球包含+模拟退火)

    Description During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found stra ...

  4. 视频播放截图及简要文字介绍——Thunder团队

    视频播放截图及简要文字介绍 图一:团队Logo ——从此我们有了自己的标志 图二:扫描本地书籍 ——可阅读本地的喜爱书籍 图三:在本地添加自己喜爱的图书 ——将自己喜爱的书籍加入书架,方便阅读 图四: ...

  5. 11.24Daily Scrum(3)

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.1002 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.1003 实现视频浏览的功能 ...

  6. laravel开发环境部署遇到的问题和个人感受

    >感受 用chrome浏览器 英语很重要 跟上更新的步伐 要不断学习 问问题要把问题描述清楚,先尝试解决,解决不了再问大佬 情绪要稳定,不能因为一个问题困扰两天就想放弃了 发现了 stack o ...

  7. 2019寒假训练营第三次作业part1-网络空间安全概论第五章

    第五章 网络攻防技术 5.1 网路信息收集技术--网络踩点 黑客入侵系统之前,需要了解目标系统可能存在的: 管理上的安全缺陷和漏洞 网络协议安全缺陷与漏洞 系统安全缺陷与漏洞 黑客实施入侵过程中,需要 ...

  8. 【Linux】- mv命令

    Linux mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法 mv [options] source dest mv [options] source... directory 参数说 ...

  9. opencv2.4.0版本不支持Mat的大小自动调整?

    在opencv2.4.9中,resize(img,img,Size(850,550))是没问题的.到了2.4.0中,要新声明一个变量Mat img1;resize(img,img1,Size(850, ...

  10. 发送tcp的时候,数据包是如何拷贝的

    发送数据包的时候,用户态的数据包是如何拷贝到内核的kiovec msghd 结构体 icmp是走sock吗? 每一个skb_buffer的大小都是固定的吗?所以有skb_available这样的函数 ...