Lightoj 1166 - Old Sorting
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 |
Output for Sample Input |
|
3 4 4 2 3 1 4 4 3 2 1 4 1 2 3 4 |
Case 1: 1 Case 2: 2 Case 3: 0 |
#include <iostream>
#include <stdio.h>
using namespace std;
int a[];
int main() {
int t, n;
cin >> t;
for(int k = ; k <= t; ++k) {
cin >> n;
for (int i = ; i <= n; ++i) {
cin >>a[i];
}
int ans = ;
for (int i = ; i <= n; ++i) {
int x = i;
int cnt = ;
while (a[i] != i) {
x = a[i];
swap(a[x], a[i]);
cnt++;
}
ans += cnt;
}
printf("Case %d: %d\n",k, ans);
}
return ;
}
Lightoj 1166 - Old Sorting的更多相关文章
- LightOJ 1166 Old Sorting 置换群 或 贪心 水题
LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...
- Old Sorting(转化成单调序列的最小次数,置换群思想)
Old Sorting Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- HDU 1166 敌兵布阵 (树状数组)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1166 敌兵布阵(分块)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
随机推荐
- Java学习之分支结构---判断语句:if语句和switch语句
一个if语句包含一个布尔表达式和一条或多条语句,if 语句的用语法如下:if 语句 if(布尔表达式) { //如果布尔表达式为true将执行的语句 },如果布尔表达式的值为 true,则执行 if ...
- css position是前端的你了解多少?
此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...
- 【转】玩玩负载均衡---在window与linux下配置nginx
最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...
- 本机操作Excel文件提示错误:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。
解决办法是: 选择项目-->右键"属性"-->生成-->目标平台-->选择X86或者首选32位
- 可拔插的 IOC 容器
可拔插的 IOC 容器 于是我打算自己实现一个这样的 bean 容器. 但在实现之前又想到一个 feature: 不如把实现 bean 容器的方案交给使用者选择,可以选择使用 bean 容器,也可以就 ...
- oracle 启动监听报错TNS-12547: TNS:lost contact
https://blog.csdn.net/liqfyiyi/article/details/7534018
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- Spring Boot使用HandlerInterceptorAdapter和WebMvcConfigurerAdapter实现原始的登录验证
HandlerInterceptorAdapter的介绍:http://www.cnblogs.com/EasonJim/p/7704740.html,相当于一个Filter拦截器,但是这个颗粒度更细 ...
- react 使用 eslint 的三种代码检查方案总结,多了解点--让代码更完美....
1.介绍 ESLint 是一个可扩展,每条规则独立,被设计为完全可配置的lint工具. 可以用来检测代码,避免低级错误 可以用来规范代码的开发风格,统一代码习惯. 2.为什么使用 ESLint ? 统 ...
- Java二维码的解码和编码
原文:http://www.open-open.com/code/view/1430906793866 import java.io.File; import java.util.Hashtable; ...