本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions)

Description

Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original inversion count.

在一个数列中选择两个数,交换他们的顺序使得逆序数变得最小,给出这个逆序数值。

Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < j and A[i] > A[j], then the pair (i, j) is called inversion of A.

定义:在数列A[n]中,如果i < j 且A[i] > A[j],那么(i, j)就是A的一个逆序。

Example:
Count(Inversion({3, 1, 2})) = Count({3, 1}, {3, 2}) = 2
InversionCountOfSwap({3, 1, 2})=>
{
 InversionCount({1, 3, 2}) = 1 <-- swapping 1 with 3, decreases inversion count by 1
 InversionCount({2, 1, 3}) = 1 <-- swapping 2 with 3, decreases inversion count by 1
 InversionCount({3, 2, 1}) = 3 <-- swapping 1 with 2 , increases inversion count by 1
}

Input

Input consists of multiple cases, one case per line.Each case consists of a sequence of integers separated by comma.

Output

For each case, print exactly one line with the new inversion count or the original inversion count if it cannot be reduced.

Sample Input

3,1,2
1,2,3,4,5

Sample Output

1
0

解题思路

对于一个数列,首先求出其逆序数。

如果逆序数不等于0(不是顺序排列),那么一定有可以减小的方法(在一个排列中对换相邻的元素,如果把小的换到大的之前,那么逆序数减一)

一种做法是:

遍历所有的数对,对比这两个数和他们之间的数,由“在一个排列中对换相邻的元素,如果把小的换到大的之前,那么逆序数减一,反之加一”计算出减少的逆序数,选择减少最多的。

Reduce inversion count 求最小逆序数的更多相关文章

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

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

  2. hdu1394Minimum Inversion Number(线段树,求最小逆序数)

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

  3. HDU-Minimum Inversion Number(最小逆序数)

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

  4. hdu-1394(线段树求最小逆序数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意: 给定一个n,然后又n个数字,首先,这些数字的大小是从0开始到n-1,比如样例n=10,则这十个数就 ...

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

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

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

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

  7. hdu1394 Minimum Inversion Number(最小逆序数)

    Minimum Inversion Number Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/O ...

  8. hdu 1394 求一个序列的最小逆序数 单点增 区间求和

    题目的意思就好比给出一个序列 如:0 3 4 1 2 设逆序数初始n = 0: 由于0后面没有比它小的,n = 0 3后面有1,2 n = 2 4后面有1,2,n = 2+2 = 4: 所以该序列逆序 ...

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

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

随机推荐

  1. zhuang 自定义Xcode代码模板:Code Snippet

    Xcode强大的代码提示功能是有目共睹的,用过都知道,可是你是否想过添加一些自定义的代码提示模板?你是否想让if默认的左括号挪到下一行?还有for.while.switch等等……如果你有这方面的需求 ...

  2. tinyxml学习4

    tinyXML一款很优秀的操作C++类库,文件不大,但方法很丰富,和apache的Dom4j可以披靡啊!习惯了使用java类库的我看到这么丰富的c++类库,很高兴!它使用很简单,只需要拷贝几个文件到你 ...

  3. Linux刷新DNS缓存

    网上查了下,发现linux刷新dns的缓存方法都是: sudo /etc/init.d/nscd restart 但是在我的机器上,发现提示命令找不到: sudo /etc/init.d/nscd: ...

  4. vbs下载文件

    Dim Url,SavePath,FilenameUrl="http://www.kumi.cn/photo/43/7e/63/437e636e413071e3.jpg"SaveP ...

  5. px,pt,em,rem

    一直对px,pt,em,rem的认识有误区,现整理一下,供参考.之后还得整理下关于dpi相关的知识. px(pixe,像素l):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物 ...

  6. MongoDB-MMS使用总结

    环境:阿里云 系统:ubuntu 12.04 数据库:MongoDB shell version: 2.0.4 登录MMS,注册相应用户 根据文档开始安装:Install the Monitoring ...

  7. Memcache的总结介绍和一些命令的总结

    1.1memcached是什么? 来看一些官方的一些解释: 1 free&opensource,high-performance,distributed memory object cachi ...

  8. COleChangeSourceDialog不能Change Source的解决方法

      在微软给的例子OClient中,有选中一个OLE对象然后Change Source的功能,但是会报错.分析了一下是这样的: void CMainView::OnOleChangeSource() ...

  9. {Reship}{C#}{GDI+}GDI+画笔,线,区域类型

    =================================================================================== This article is ...

  10. C# 定制 Attribute 简单使用

    所谓 “定制Attribute”,就是一个类的实例,它被序列化成驻留在元数据的一个字节流. 我们可以使用 Attribute 来保存注释: namespace AttributeDemo { [Att ...