Flip Sort

Sorting in computer science is an important part. Almost every problem can be solved effeciently if sorted data are found. There are some excellent sorting algorithm which has already acheived the lower bound nlgn. In this problem we will also discuss about a new sorting approach. In this approach only one operation ( Flip ) is available and that is you can exchange two adjacent terms. If you think a while, you will see that it is always possible to sort a set of numbers in this way.

The Problem

A set of integers will be given. Now using the above approach we want to sort the numbers in ascending order. You have to find out the minimum number of flips required. Such as to sort "1 2 3" we need no flip operation whether to sort "2 3 1" we need at least 2 flip operations.

The Input

The input will start with a positive integer N ( N<=1000 ). In next few lines there will be N integers. Input will be terminated by EOF.

The Output

For each data set print "Minimum exchange operations : M" where M is the minimum flip operations required to perform sorting. Use a seperate line for each case.

Sample Input

3
1 2 3
3
2 3 1

Sample Output

Minimum exchange operations : 0
Minimum exchange operations : 2

 

 

相邻交互排序最少交换次数 是 逆序对

冒泡排序的交换次数是逆序对

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1001;
int a[maxn];
int n; //两重循环进行枚举 计算逆序对
int solve()
{
int cnt=0;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
cnt++;
return cnt;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva10327.in", "r", stdin);
#endif
while(scanf("%d", &n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d", a+i);
}
printf("Minimum exchange operations : %d\n", solve());
}
return 0;
}

uva10327 - Flip Sort的更多相关文章

  1. 10327 - Flip Sort

    原文链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&pa ...

  2. Zerojudge解题经验交流

    题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...

  3. pancake sort的几个问题

    1. 每次找剩下序列中的最大值,可以进行pancake sort,时间复杂度是O(n^2): 2. 求最少交换次数进行pancake sort的问题是个NP问题,搜索的时候,upper bound是2 ...

  4. hdu-1890-Robotic Sort splay区间翻转

    题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...

  5. 数据结构(Splay平衡树):HDU 1890 Robotic Sort

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. 【agc019D】Shift and Flip

    Portal --> agc019D Description 给你一个\(A\)串一个\(B\)串(长度相等的两个\(01\)串),一次操作可以选择将\(A\)向左循环移动一位,将\(A\)向右 ...

  7. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  8. [算法]——快速排序(Quick Sort)

    顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...

  9. shell之sort命令

    1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [rocrocket@rocrocket progr ...

随机推荐

  1. 设计模式-单键(Singleton)

    [摘要]   在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性.以及良好的效率. 如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例? 这应该 ...

  2. 什么是SPF?如何设置企业邮箱的SPF呢?(TXT记录)

    什么是SPF?   (Sender Policy Framework) 的缩写,一种以IP地址认证电子邮件发件人身份的技术,是非常高效的垃圾邮件解决方案. 接收邮件方会首先检查域名的SPF记录,来确定 ...

  3. [转]linux 如何改变文件属性与权限

    转自:http://www.cnblogs.com/yangjinjin/p/3165076.html 我们知道档案权限对于一个系统的安全重要性,也知道档案的权限对于使用者与群组的相关性, 那如何修改 ...

  4. EFSQLserver

    1.增加一条烽据 FLYNEWQKEntities dataContext = new FLYNEWQKEntities(); Log log = new Log(); log.Data1 = &qu ...

  5. Enter回车切换输入焦点方法兼容各大浏览器

    做项目时,客户要求能够用enter回车直接切换输入(焦点),当最后一个时候,直接提交信息. 第一想法就是,网上去copy一段代码直接用.但了百度.谷歌找了个遍,找到的代码80%以上都是一样的.有的代码 ...

  6. EditText的几个小点

    1. EditText 由 TextView 继承而来 2. android中inputType属性在EditText输入值时启动的虚拟键盘的风格有着重要的作用.这也大大的方便的操作.有时需要虚拟键盘 ...

  7. Python 最佳实践

    前言 对我来说,以前每次面试是我审视自己,检验自己的一种方式.每次准备面试,以及被面试官问住的时候才会发现,其实我python我学的还不够好.工作中也是,可以从其他的同事那里获得成长.但是我今天说的是 ...

  8. [转]Python文件操作

    前言 这里的“文件”不单单指磁盘上的普通文件,也指代任何抽象层面上的文件.例如:通过URL打开一个Web页面“文件”,Unix系统下进程间通讯也是通过抽象的进程“文件”进行的.由于使用了统一的接口,从 ...

  9. 第二百零四天 how can i 坚持

    我应该不会看错吧.最近媒体热炒小米衰落了,有必要那么大张旗鼓的报道吗?小米.华为,坚决看好小米.感觉华为品牌有些杂乱,在走三星的老路,小米有些苹果的影子,但是,多了个互联网.互联网... 未来孰优孰劣 ...

  10. Linux下的grep搜索命令详解(一)

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...