Minimum Inversion Number

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

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
题意:求不同数列中最小的逆序对数。
思路:
      统计a[i]前面的,且比它大的数
      这样做的话,就可以利用输入的时效性,每输入一个数,就把这个数的num[i]值为1,

   然后统计比这个数大的数的num和,
   因为这里的和一定是在这个数列中比a[i]大,且在它前面出现的数之和,
   然后把把这个和加到总逆序数sum里。
   这样做的话直接的暴力作法依然是n2,但是,
   我们可以在,统计比这个数大的数的num和这一步进行优化,利用线段树求区间域值的复杂度是logn,
   所以总体复杂度就降到了nlogn。
  
再来看这道题,求得初始数列的逆序数后,再求其他排列的逆序数有一个规律,就是
   sum = sum + (n - 1 - a[i]) - a[i];比如原来的逆序数是sum,把a[0]移到最后后,减少逆序数a[0],同时增加逆序数n-a[0]-1个。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 5500
int tre[*maxn]; void update(int num, int le, int ri, int x)
{
if(le == ri)
{
tre[num] = ;
return;
}
int mid = (le+ri)/;
if(x<=mid)
update(num*,le,mid,x);
else
update(num*+,mid+,ri,x);
tre[num] = tre[num*] + tre[num*+];
}
int query(int num, int le, int ri, int x, int y)
{
if(x<=le&&y>=ri)
{
return tre[num];
}
int mid = (le+ri)/;
int ans = ;
if(x<=mid)
ans += query(num*,le,mid,x,y);
if(y>mid)
ans += query(num*+,mid+,ri,x,y);
return ans;
}
int a[maxn];
int main()
{
int n;
while(~scanf("%d", &n))
{
memset(tre, , sizeof tre);
int sum = ; for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
update(,,n-,a[i]); sum += query(,,n-,a[i]+,n-);
} int ans = sum;
for(int i = ; i < n; i++)
{
sum = sum - a[i] + (n--a[i]);
ans = min(ans, sum);
}
printf("%d\n", ans);
}
return ;
}
方法二:归并排序求逆序对。
 
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 5500 int cnt;
int t[maxn],a[maxn],b[maxn];
void merge_sort(int x,int y)//归并排序模板
{
if(y-x>)
{
int m=x+(y-x)/;
int p=x,q=m,i=x;
merge_sort(x,m);
merge_sort(m,y);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q]))
t[i++]=a[p++];
else
{
t[i++]=a[q++];
cnt+=m-p;
}
}
for(i=x;i<y;i++)
a[i]=t[i];
}
}
int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
cnt = ;
merge_sort(,n);
int ans = cnt;
for(int i = ; i < n; i++)
{
cnt = cnt - b[i] + (n--b[i]);
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return ;
}

HDU1394 Minimum Inversion Number(线段树OR归并排序)的更多相关文章

  1. hdu1394(Minimum Inversion Number)线段树

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

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

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

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

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

  4. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

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

  5. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

  6. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  7. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  8. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

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

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

随机推荐

  1. Django架设blog步骤

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  2. 【Eclipse DDMS】 Can't bind to local 8600 for debugger

    问题原因: 电脑上同时安装了Eclipse 和Android Studio两个ide. 关键是使用eclipse adb连接真机时候,android studio也处于运行状态,后者默认也是要连接ad ...

  3. C++ multimap 的插入,遍历,删除

    #include <iostream> #include <map> #include <string> using namespace std; int main ...

  4. [RxJS] Basic DOM Rendering with Subscribe

    While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...

  5. javasscript学习笔记 之 数组学习二 数组的所有方法

    1.push() 和 pop()  栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...

  6. .NET基础拾遗(3)字符串、集合和流2

    二.常用集合和泛型 2.1 int[]是值类型还是引用类型? .NET中无论是存储值类型对象的数组还是存储引用类型的数组,其本身都是引用类型,其内存也都是分配在堆上的.所有的数组类型都继承自Syste ...

  7. .NET基础拾遗(3)字符串、集合和流1

    一.字符串处理 1.1 StringBuilder类型 众所周知,在.NET中String是引用类型,具有不可变性,当一个String对象被修改.插入.连接.截断时,新的String对象就将被分配,这 ...

  8. Power Designer - 反向获取数据库物理模型时Unable to list the users 异常

    解决方案: 菜单栏 -> Database -> Change Current DBMS ,给DBMS选择Oracle Version 9i2.

  9. FileShare文件读写锁解决“文件XXX正由另一进程使用,因此该进程无法访问此文件”(转)

    开发过程中,我们往往需要大量与文件交互,读文件,写文件已成家常便饭,本地运行完美,但一上到投产环境,往往会出现很多令人措手不及的意外,或开发中的烦恼,因此,我对普通的C#文件操作做了一次总结,问题大部 ...

  10. (转).net开发者对android开发一周的学习体会

    春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...