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线程和多线程(二)——对象中的wait,notify以及notifyAll方法
Java对象中的wait,notify以及notifyAll方法 在Java的Object类中包含了3个final的方法,这三个方法允许线程来交流资源是否被锁定.这三个方法就是wait(),notif ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
- Fiddler抓包-get与post请求
from:https://www.cnblogs.com/yoyoketang/p/6719717.html 本篇以博客园的请求为例,简单分析get与post数据有何不一样,以后也能分辨出哪些是get ...
- 软件测试人员遇到发现的bug不能重现怎么办?
软件测试人员遇到发现的bug不能重现怎么办? 刚刚进入测试的童鞋们,想必都遇到过提出的bug,开发要求重现之后,但是在系统上已经重现不了的情况吧. 那么碰到这样的情况,不管开发还是测试都很纠结,开 ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人 ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
- ES6__class 的继承等相关知识案例
/** * class 的继承等相关知识 */ // extends. static. super const canvas = document.querySelector('#canvas'); ...
- codevs——3064 求和
3064 求和 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 输入一个数x(x <= ...
- POJ 2337 【字典序】【欧拉回路】
题意: 给你一些单词,判断这些单词能否在保证首尾单词相同的情况下连成一排. 如果有多组解,输出字典序最小的一组解. 这题... WA了两天. 错误有以下: 1.没有初始化好起始位置,默认起始位置是a了 ...