LightOJ - 1166 Old Sorting (置换群)
来源:互联网 发布:淘宝流量公式 编辑:程序博客网 时间:2024/05/22 10:49
Submit Status
Description
Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to sort the array in ascending order. A swap means, you can exchange any two elements of the array.
For example, let n = 4, and the array be 4 2 3 1, then you can sort it in ascending order in just 1 swaps (by swapping 4 and 1).
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains two lines, the first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers separated by spaces. You may assume that the array will always contain a permutation of 1 to n.
Output
For each case, print the case number and the minimum number of swaps required to sort the array in ascending order.
Sample Input
3
4
4 2 3 1
4
4 3 2 1
4
1 2 3 4
Sample Output
Case 1: 1
Case 2: 2
Case 3: 0
Source
#include<stdio.h>#include<string.h>#include<algorithm>#define N 110using namespace std;struct zz{int x;int id;}p[N];bool cmp(zz a,zz b){return a.x<b.x;}int a[N],vis[N];int main(){int t,T=1,n,i,j,k;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i]);p[i].x=a[i];p[i].id=i;}sort(p+1,p+n+1,cmp);int cnt,sum=0;memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){cnt=0;k=p[i].x;if(!vis[k]){while(a[i]!=k){vis[k]=1;vis[a[i]]=1;k=p[k].id;cnt++;}sum+=cnt;}}printf("Case %d: %d\n",T++,sum);}return 0;}
- LightOJ - 1166 Old Sorting (置换群)
- 【LightOJ】1166 - Old Sorting(置换群)
- lightoj 1166 - Old Sorting 【置换群】
- Lightoj 1166 - Old Sorting (简单贪心)
- Lightoj 1166 Old Sorting(大水题)
- 1166 Sorting LightOJ【置换群基础】
- LightOJ - 1166 Old Sorting(贪心)
- lightoj 1166 1166 - Old Sorting 贪心
- LightOJ 1166 Old sorting(贪心/模拟?)
- poj3270--Cow Sorting(置换群)
- 【POJ 2370】Cow Sorting(置换群)
- poj_3270 Cow Sorting(置换群)
- POJ 3270 Cow Sorting (置换群)
- poj3270Cow Sorting【置换群】
- Cow Sorting(置换)
- poj3270 Cow Sorting-------置换群
- poj3270 Cow Sorting 置换群
- Cow Sorting(DP之—置换群)
- 解决 oracle没有Scott用户 的问题
- 第八周项目三 指向学生类的指针
- 项目1——数组作数据成员
- 【HNOI2016】【BZOJ4540】序列
- Linux使用imagemagick的convert命令压缩图片,节省服务器空间
- LightOJ - 1166 Old Sorting (置换群)
- 第六周项目2-带武器的游戏角色
- AE编辑-显示、增加、删除节点
- C++第四次作业
- c++上机实验4项目1-利用循环求和
- 【搜索】哈密顿绕行世界问题
- 第五周项目一使用默认带参数的构造函数定义三角形的类
- 【bzoj1907】【树的路径覆盖】【贪心】
- memcache使用及语法