UVA 331 Mapping the Swaps
来源:互联网 发布:centos在线安装jdk1.7 编辑:程序博客网 时间:2024/06/03 06:06
Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs to be swapped, in the sequence they are to be swapped, we obtain what might be called a swap map. For example, suppose we wish to sort the array A whose elements are 3, 2, and 1 in that order. If the subscripts for this array are 1, 2, and 3, sorting the array can be accomplished by swapping A2 and A3, then swapping A1 and A2, and finally swapping A2 and A3. If a pair is identified in a swap map by indicating the subscript of the first element of the pair to be swapped, then this sorting process would be characterized with the swap map 2 1 2.
It is instructive to note that there may be many ways in which swapping of adjacent array entries can be used to sort an array. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then swapping A2 and A3, and finally swapping A1 and A2 again. The swap map that describes this sorting sequence is 1 2 1.
For a given array, how many different swap maps exist? A little thought will show that there are an infinite number of swap maps, since sequential swapping of an arbitrary pair of elements will not change the order of the elements. Thus the swap map 1 1 1 2 1 will also leave our array elements in ascending order. But how many swap maps of minimum size will place a given array in order? That is the question you are to answer in this problem.
Input
The input data will contain an arbitrary number of test cases, followed by a single 0. Each test case will have a integer n that gives the size of an array, and will be followed by the n integer values in the array.
Output
For each test case, print a message similar to those shown in the sample output below. In no test case will n be larger than 5.
Sample Input
2 9 72 12 503 3 2 13 9 1 50
Sample Output
There are 1 swap maps for input data set 1.There are 0 swap maps for input data set 2.There are 2 swap maps for input data set 3.There are 1 swap maps for input data set 4.
求最少交换次数使得数组有序,首先冒泡法的次数是最少的,接下来只要暴力即可。
#include<cstdio>#include<cstring>#include<iostream>#include<string>#include<algorithm>using namespace std;const int maxn = 10;int n;int inv, a[maxn], ans;void dfs(int d){if (d == inv) {for (int i = 0; i<n - 1; i++) if (a[i]>a[i + 1]) return;ans++;}else for (int i = 0; i<n - 1; i++) {swap(a[i], a[i + 1]);dfs(d + 1);swap(a[i], a[i + 1]);}}int main(){int kase = 0;while (scanf("%d", &n) == 1 && n) {for (int i = 0; i<n; i++) scanf("%d", &a[i]);inv = 0;for (int i = 0; i<n; i++)for (int j = i + 1; j<n; j++) if (a[i]>a[j]) inv++;ans = 0;if (inv > 0) dfs(0);printf("There are %d swap maps for input data set %d.\n", ans, ++kase);}return 0;}
- uva 331 Mapping the Swaps
- uva 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- uva 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVA 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVA 331 Mapping the Swaps
- uva 331 Mapping the Swaps
- uva 331 Mapping the Swaps
- uva 331 Mapping the Swaps
- UVa:331 Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVa 331Mapping the Swaps
- uva 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVA 331 - Mapping the Swaps
- UVA - 331 Mapping the Swaps
- Struts2结果类型的对比(redirect)与(redirectAction)
- 计算机与接口通信期末作业(国软)
- 备用
- hdu 1160 DP
- Sort Colors
- UVA 331 Mapping the Swaps
- [leetcode] 39& 40 Combination Sum I& II
- 树链剖分模板(基于点权,基于边权)
- UVA 10012 How Big Is It?
- Java 基于socket thread 简单聊天程序
- Andrid 之自动化测试工具 Monkeyrunner
- USACO 1.5 prime Palindromes 回文质数(vector的使用)
- uva 10405 Longest Common Subsequence
- UVA 167 The Sultan's Successors