uva10327 - Flip Sort

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,布布扣,bubuko.com

时间: 2024-10-12 17:53:14

uva10327 - Flip Sort的相关文章

第二篇 线性数据结构的编程实验 第7章 应用线性表排序编程

7.1 利用STL中自带的排序功能编程的实验范例    7.1.1 Hardwood Species    7.1.2 Who's in the Middle    7.1.3 ACM Rank Table  7.2 应用排序算法编程的实验范例 7.2.1 Flip Sort 7.2.2 Ultra-QuickSort 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/10400271.html

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

Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3456    Accepted Submission(s): 1493 Problem Description Somewhere deep in the Czech Technical University buildings, there are labora

codeforces Gravity Flip 题解

Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity. There are n columns of toy cubes in the box arranged in a line. The i-th

经典排序算法 - 冒泡排序Bubble sort

 原文出自于 http://www.cnblogs.com/kkun/archive/2011/11/23/bubble_sort.html 经典排序算法 - 冒泡排序Bubble sort 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子为从小到大排序, 原始待排序数组| 6 | 2 | 4 | 1 | 5 | 9 | 第一趟排序(外循环) 第

Redis学习之Sort Set详解

本文和大家分享的主要是Redis中Sort Set相关内容,一起来看看吧,希望对大家学习redis有所帮助. 游戏服务器需要做一个排行榜实时更新,如果沿用传统的方法,一般是通过后端的定时任务去跑数据来生成排行榜数据,这种方法一方面无法满足产品对功能实时性的要求,另一方面也一定程度上消耗服务器端有限的资源.如果从每次数据库读取数据并进行排名(使用Mysql的sort关键字进行排序),在关卡数据量的级数大时是一种效率低的方法.在查阅大量资料后,发现了Redis中的有序集合(Sort Set). Re

75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[BZOJ1552][Cerc2007]robotic sort

试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. 输出 输出共一行,N个用空格隔开的正整数P1,P2,P3-Pn,Pi表示第i次操作前第i小的物品所在的位置. 注意:如果第i次操作前,第i小的物品己经在正确的位置Pi上,我们将区间[Pi,Pi]反转(单个物品). 输入示例 6 3 4 5 1 6 2 输出示例 4 6 4 5 6 6 数据规模及约定 见"输入" 题解 暴力

PROC SORT

PROC SORT DATA=输入数据集 <OUT=输出数据集><选项>; BY 关键变量; RUN; PROC SORT 语法如上,其中在尖括号中的可以不出现,也不会报错. 选项中常用的有两种NODUPKEY和NOUNIQUEKEY. 功能可以由字面意思帮助记忆,NO-没有,DUP-重复,KEY-关键变量,,,合起来就是数据集中指定的关键变量不重复, 其功能也一样,,去除重复的关键变量,这里的关键变量就是指跟在关键字BY后面的变量:还有一个对应的DUPOUT=选项用来输出被该(N

PAT甲题题解-1101. Quick Sort (25)-大水题

快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着循环一次,统计出代码中的minnum和maxnum即可,注意最后一定要输出'\n',不然第三个测试会显示PE,格式错误. #include <iostream> #include <cstdio> #include <algorithm> #include <map&