uva10327 - 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的更多相关文章
- 10327 - Flip Sort
原文链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&pa ...
- Zerojudge解题经验交流
题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...
- pancake sort的几个问题
1. 每次找剩下序列中的最大值,可以进行pancake sort,时间复杂度是O(n^2): 2. 求最少交换次数进行pancake sort的问题是个NP问题,搜索的时候,upper bound是2 ...
- hdu-1890-Robotic Sort splay区间翻转
题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...
- 数据结构(Splay平衡树):HDU 1890 Robotic Sort
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【agc019D】Shift and Flip
Portal --> agc019D Description 给你一个\(A\)串一个\(B\)串(长度相等的两个\(01\)串),一次操作可以选择将\(A\)向左循环移动一位,将\(A\)向右 ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- [算法]——快速排序(Quick Sort)
顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...
- shell之sort命令
1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [rocrocket@rocrocket progr ...
随机推荐
- aspose.word 在书签处插入符号
doc.Range.Bookmarks["CBJYQQDFS110"].Text = ""; Aspose.Words.DocumentBuilder buil ...
- XSS 前端防火墙(5): 整装待发
到目前为止,我们把能用前端脚本防御 XSS 的方案都列举了一遍. 尽管看起来似乎很复杂累赘,不过那些是理论探讨而已,在实际中未必要都实现.我们的目标只是为了预警,能发现问题就行,并非要做到滴水不漏的程 ...
- JVM 性能调优实战之:一次系统性能瓶颈的寻找过程
玩过性能优化的朋友都清楚,性能优化的关键并不在于怎么进行优化,而在于怎么找到当前系统的性能瓶颈.性能优化分为好几个层次,比如系统层次.算法层次.代码层次…JVM 的性能优化被认为是底层优化,门槛较高, ...
- 更新的packages.config所有的软件包?
1. 你NuGet.exe的恢复你的包.运行的每个项目. nuget install packages.config 或用NuGet的2.7可以恢复所有包中的行. nuget restore Your ...
- DouNet学习_Excel导入导出
Excel --->列是有限的 -->数据靠在单元格右边是数字类型,左边是字符串类型 把一个数字当初字符串来显示 在前面加个 ' -->用程序操作Excel 可以使用Excel的所有 ...
- MFC中GetPrivateProfileString相关函数
项目中用到了这个函数,所以了解了一下,参考了一些博客: http://blog.sina.com.cn/s/blog_a599b5960101tsbk.html http://blog.csdn.ne ...
- arp spoofing
Today our tutorial will talk about Kali Linux Man in the Middle Attack. How to perform man in the mi ...
- FlatBuffers要点
FlatBuffers发布出来一周多,周末便抽时间先研究下它的使用方法.Flatbuffers的idl的语法主要参考[http://google.github.io/flatbuffers/md__s ...
- String - 兴趣解读
个优点: . 以下代码的HashCode是否相同,它们是否是同个对象: . 以下代码的HashCode是否相同,他们是否是同个对象: . 以下代码的HashCode是否相同,他们是否是同 ...
- geeksforgeeks@ Equal to product (Binary Search)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=667 Equal to product Given an array of in ...