UVa 299 - Train Swapping

题目:给你一个序列,每次只能交换相邻的两个数,问使得序列递增的最小的交换次数。

分析:数学,逆序数。

如果在一个序列中存在a[i] > a[j]并且 i < j,则a[i]与a[j]构成一对逆序对;

一个序列的逆序对的总数,就是这个序列的逆序数;

如题,相邻元素交换,每次交换序列逆序数必然改变1,而一个递增的序列逆序数为0;

因此,最少的交换次数即为逆序数,而每次按照逆序对减少的方式交换就得到递增序列。

说明:整理很久以前刷过的题目。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int data[100];

int main()
{
	int n,m,count;
	while (cin >> n)
	while (n --) {
		cin >> m;
		for (int i = 0 ; i < m ; ++ i)
			cin >> data[i];
		count = 0;
		for (int i = 0 ; i < m ; ++ i)
		for (int j = 0 ; j < i ; ++ j)
			count += (data[j]>data[i]);

		cout << "Optimal train swapping takes " << count << " swaps." << endl;
	}
	return 0;
}
时间: 2024-12-04 20:57:27

UVa 299 - Train Swapping的相关文章

UVa 11586 - Train Tracks

题目:给你一些积木碎片,每个碎片的两端只能是凸或凹(M或F),凸凹可拼起来,能否拼成一个环. 分析:图论,欧拉回路.判断入度等于出度即可,即M和F相同且大于1组. 说明:╮(╯▽╰)╭. #include <cstring> #include <cstdio> char buf[202]; int main() { int n; while (~scanf("%d",&n)) { getchar(); while (n --) { gets(buf);

小白书练习题5.5.3 排序检索类、

UVA 340 Master-Mind Hints 题意:猜数字游戏,给n个数的序列给你.接下来一行是答案序列.剩下的都是猜测序列.对于每一个猜测序列,统计有多少个数字相同并且位置相同.有多少数字相同位置不同.每一个数字只能用一次. 思路:直接统计可以求出数字相同并且位置相同的哪一些数.在此过程中我加了一个标记数组.标记那些用过的数的位置为1,没用过为0:然后枚举猜测中哪些没用过的数字.去答案序列中找.当数字相等并且答案行中那个数也没用过时.计数加1: 1 #include<cstdio> 2

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

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 em

Volume 1. Elementary Problem Solving :: Sorting/SearchingUva 340,10420,10474,152,299,120,156,400,755

刘汝佳 算法入门 第一版 Uva题目集合(四) Uva 340 #include<stdio.h> #include<string.h> int h[1001][2],g[1001]={0}; int n,m=0,i,j,k,a,b,o; int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","

sort题目

uva,146 全排列问题:permutation 具体详解:参考Devymex UVa Problem 146 - ID Codes Problem: Please find the problem here. Solution: This is simply the next permutation problem. Thanks to the hint on Competitive Programming, there is a simple call in C++ library nam

寒假练习 04

断断续续的把排序和检索专题刷完了,感觉英语还是不够,题目太长以后看起来就会很吃力. 还有一点感触就是STL的广泛应用.学到了很多新东西. 当然,不能忍受的就是答案最后多输出一行空行,UVaOJ会判WA. UVaOJ 340 简单模拟题,一开始没有看懂题目.百度以后才明白的题意. #include <iostream> #include <memory.h> using namespace std; const int MAX = 1024; int pCode[MAX], pGue

算法入门系列之排序与检索

UVA340   UVA10420 时间有点久远,很早之前写的,然后忘记总结了,这道题其实很容易,一行只取第一个字符串,然后按照字典序输出每个字符串的个数. 这里有个技巧就是先用scanf获取第一个字符串,然后再用gets直接吸收剩下的字母.其次就是用map记录个数,然后用迭代器输出结果 #include<iostream> #include<cstring> #include<cstdio> #include<map> using namespace st

UVA 12123 - Magnetic Train Tracks(计数问题)

题目链接:12123 - Magnetic Train Tracks 题意:给定n个点,求有几个锐角三角形. 思路:和UVA 11529是同类的题,枚举一个做原点,然后剩下点根据这个原点进行极角排序,然后利用two pointer去遍历一遍,找出角度小于90度的锐角,然后扣掉这些得到钝角三角形的个数,然后在用总情况去扣掉钝角就是锐角或直角 代码: #include <stdio.h> #include <string.h> #include <math.h> #incl