poj 1654--重新排序求交换的次数
2013-03-04 19:22
225 查看
把一个序列打乱,问从小到大排序后,需要经过几次交换;
Sorting by Swapping
Description
Given a permutation of numbers from 1 to n, we can always get the sequence 1, 2, 3, ..., n by swapping pairs of numbers. For example, if the initial sequence is 2, 3, 5, 4, 1, we can sort them in the following way:
2 3 5 4 1
1 3 5 4 2
1 3 2 4 5
1 2 3 4 5
Here three swaps have been used. The problem is, given a specific permutation, how many swaps we needs to take at least.
Input
The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each case contains two lines. The first line contains the integer n (1 <= n <= 10000), and the second line gives the initial permutation.
Output
For each test case, the output will be only one integer, which is the least number of swaps needed to get the sequence 1, 2, 3, ..., n from the initial permutation.
Sample Input
Sample Output
代码:
Sorting by Swapping
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:8948 | Accepted: 4747 |
Given a permutation of numbers from 1 to n, we can always get the sequence 1, 2, 3, ..., n by swapping pairs of numbers. For example, if the initial sequence is 2, 3, 5, 4, 1, we can sort them in the following way:
2 3 5 4 1
1 3 5 4 2
1 3 2 4 5
1 2 3 4 5
Here three swaps have been used. The problem is, given a specific permutation, how many swaps we needs to take at least.
Input
The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each case contains two lines. The first line contains the integer n (1 <= n <= 10000), and the second line gives the initial permutation.
Output
For each test case, the output will be only one integer, which is the least number of swaps needed to get the sequence 1, 2, 3, ..., n from the initial permutation.
Sample Input
2 3 1 2 3 5 2 3 5 4 1
Sample Output
0 3
代码:
#include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cmath> #include<queue> using namespace std; #define sf scanf #define pf printf #define INF 1<<29 #define eps 1e-6 const double PI=acos(-1.0); #define lint __int64 #define LL long long const int tt=10000; #define ULLint unsigned long long //2^64-1>1.8*10^19 #define clr(x) memset(x,0,sizeof(x)) #define Clr(x) memset(x,-1,sizeof(x)) int n; int a[10004]; int b[10004]; int main(){ clr(a); clr(a); int cas; sf("%d",&cas); while(cas--){ sf("%d",&n); for(int i=0;i<n;i++){ sf("%d",&a[i]); b[i]=a[i]; } sort(b,b+n); int sum=0; for(int i=0;i<n;i++){ if(a[i]!=b[i]){ for(int j=i+1;j<n;j++){ if(a[j]==b[i]){ swap(a[i],a[j]); sum++; } } } } pf("%d\n",sum); } return 0; }
相关文章推荐
- ACM: 计算交换次数的排序题 poj 22…
- 通过交换相邻数来完成排序所需要的最少交换次数
- Cycle Sort (交换次数最少的排序)
- poj 2299 交换的最少次数 归并排序
- POJ Ultra-QuickSort(2299)-用归并排序求min交换次数
- 在控制台中输入一个数组,并对这个数组进行重新排序,将第一个数和最后一个交换,将第二个和倒数第二个交换
- 排序中求最小交换次数
- 编写一个js函数求一个字符串数组按字符串中各字母出现次数多少的重新排序
- 排序区大小对于排序时产生的内存与文件交换I/O次数影响
- 面试题笔试题——数组排序交换次数
- 交换任意两个元素进行排序进行的最小交换次数
- 数组排序--计算最小交换次数
- 交换次数最少的排序
- POJ 3270-Cow SortingA(Polya定理-交换次数)
- Baidu笔试-01序列排序的交换次数
- [阿里一道笔试题]排序的交换次数
- 只含0、1的串进行排序,可任意交换位置,最小交换次数
- UVA331求排序过程交换次数最少的种类数
- (使用STL自带的排序函数进行排序7.3.6)POJ 2945 Find the Clones拷贝相同次数的文本出现了多少次()
- 冒泡排序的交换次数