UVA 299- Train Swapping(冒泡排序中交换次数)

Train Swapping

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

Description

 Train Swapping 

At an old railway station, you may still encounter one of the last remaining ``train swappers‘‘. A train swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.

Once the carriages are arranged in the optimal order, all the train driver has to do, is drop the carriages off, one by one, at the stations for which the load is meant.

The title ``train swapper‘‘ stems from the first person who performed this task, at a station close to a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of
the river. After rotating the bridge 90 degrees, boats could pass left or right.

The first train swapper had discovered that the bridge could be operated with at most two carriages on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the carriages
(as a side effect, the carriages then faced the opposite direction, but train carriages can move either way, so who cares).

Now that almost all train swappers have died out, the railway company would like to automate their operation. Part of the program to be developed, is a routine which decides for a given train the least number
of swaps of two adjacent carriages necessary to order the train. Your assignment is to create that routine.

Input Specification

The input contains on the first line the number of test cases (N). Each test case consists of two input lines. The first line of a test case contains an integer L, determining the length of
the train (  ). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current
order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc. with carriage L coming last.

Output Specification

For each test case output the sentence: ‘Optimal train swapping takes S swaps.‘ where S is an integer.

Example Input

3
3
1 3 2
4
4 3 2 1
2
2 1

Example Output

Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.

题意:就是简单的冒泡排序,让你求出冒泡排序过程中交换的次数

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int a[110];
    int T,n,t,i,j;
    scanf("%d",&T);
    while(T--)
    {
        int cnt=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
                cnt++;
            }
        }
        printf("Optimal train swapping takes %d swaps.\n",cnt);
    }
    return 0;

}
时间: 2024-08-07 21:20:05

UVA 299- Train Swapping(冒泡排序中交换次数)的相关文章

UVa 299 - Train Swapping

题目:给你一个序列,每次只能交换相邻的两个数,问使得序列递增的最小的交换次数. 分析:数学,逆序数. 如果在一个序列中存在a[i] > a[j]并且 i < j,则a[i]与a[j]构成一对逆序对: 一个序列的逆序对的总数,就是这个序列的逆序数: 如题,相邻元素交换,每次交换序列逆序数必然改变1,而一个递增的序列逆序数为0: 因此,最少的交换次数即为逆序数,而每次按照逆序对减少的方式交换就得到递增序列. 说明:整理很久以前刷过的题目. #include <iostream> #in

冒泡排序的交换次数 (树状数组)

计算冒泡排序的交换次数: 逆序数概念:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序 一个排列中所有逆序总数叫做这个排列的逆序数. 所以冒泡排序结束即是所有的逆序数为0 思路: 暴力:我们可以开一个vis[]数组记录 在遍历到第 i 位时,已经出现的值有哪些.然后遍历到 arr[i] (第i位的值),得到小于arr[i] 的出现个数(即 i<j && arr[i]<= arr[j]的个数) 然后再用当前长度 i 减去符合条件个数

冒泡排序的交换次数

题意: 给定一个1~n的排列a0,a1,-an-1,求对这个数列进行冒泡排序所需要的交换次数(冒泡排序是每次找到满足ai>ai+1的i,并交换ai和ai+1,直到这样的i不存在为止的算法). 限制条件:1<= n<= 100000 输入: n=4, a={3,1,4,2} 输出: 3 冒泡排序的复杂度是O(n2),所有无法通过模拟冒泡排序的过程来计算需要的交换次数.不过我们可以通过选取适当的数据结构来解决这个问题. 首先,所求的交换次数等价于满足i<j,ai>aj的(i,j)

【算法28】冒泡排序中的交换次数问题

问题描述 题目来源:Topcoder SRM 627 Div2 BubbleSortWithReversals 给定待排序数组A,在最多反转K个A的不相交子数组后,对A采用冒泡排序,问最小的swap次数是多少?冒泡排序的伪代码如下: BubbleSort(A): 循环len(A) - 1次: for i from 0 to len(A) - 2: if (A[i] > A[i+1]) swap(A[i], A[i+1]) 问题分析 首先,容易分析得到:对于任意待排序数组A,其采用冒泡排序所需要的

POJ 2299-Ultra-QuickSort(归并排序求相邻元素的交换次数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 43816   Accepted: 15979 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

47. 数组中出现次数超过一半的数字[Number appears more than half times]

[题目]:数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字. 例如长度为9的数组{1,2,3,2,2,2,5,4,2}中次数超过了数组长度的一半的数字为2,而长度为8的数组{1,2,3,2,2,2,5,4}则为非法输入. [思路一]:先对数组进行排序,再遍历排序后的数组,统计每个数的次数,出现次数最大的数即为要找的数. 时间复杂度:O(nlgn)+ O(n)= O(nlgn):空间复杂度:O(1). [思路二]:先对数组进行排序,出现次数超过数组长度的一半的数必然是数组中间的那个数.

Cycle Sort (交换次数最少的排序)

该算法的效率并不高.但是却提供了一个很好的思路.如何让一个序列在最小交换次数下实现有序. Cycle Sort 翻译成中文是 圈排序. 这个圈在于需要交换的数据形成圈. 具体一点: 如: Array  4 3 2 5 5 6  要处理的数组 Result 2 3 4 5 5 6  结果 pos     0 1 2 3 4 5  下标 从下标0的元素开始观察.4 需要到下标 2 而下标2的元素为 2 需要到下标0 .刚好可以回到4. 也就是形成了 4-2 这样的圈 接下来是3 需要到下标1 而3本

关于一个求最小交换次数的算法的一个严格证明,是严格证明,不是想当然

问题描述: 有一个1~n的数列的排列,但是这个数列已经被打乱了排列顺序,如果我们只是通过"交换任意两个元素",那么,要实现元素从1~n的有序排列,"最少的交换次数是多少?" 解答过程: 首先我们纸上可以先写写简单的情况试试,比如排列:4 3 1 2, 交换次数=3:我们可以在多组测试中,边测试边想,真正的实现需要满足:4本该到2处, 2本该到3处, 3本该到1处, 1本该到4处,刚好一个循环. 于是,考虑: 引理:是否对于满足这种一个循环的排列,最少交换次数等于元素

剑指Offer--029-数组中出现次数超过一半的数字

链接 牛客OJ:数组中出现次数超过一半的数字 九度OJ:http://ac.jobdu.com/problem.php?pid=1370 GitHub代码: 029-数组中出现次数超过一半的数字 CSDN题解:剑指Offer–029-数组中出现次数超过一半的数字 牛客OJ 九度OJ CSDN题解 GitHub代码 数组中出现次数超过一半的数字 1370-数组中出现次数超过一半的数字 剑指Offer–029-数组中出现次数超过一半的数字 029-数组中出现次数超过一半的数字 题意 题目描述 输入一