Problem Description
Kayaking is playing a puzzle game containing n different blocks. He marks the blocks with integers from 1 to n, which show the blocks’ original positions. Each time he can exchange two blocks and he wants to know how many times he needs at least to restore the puzzle.
 
Input
The input starts with one line contains exactly one
positive integer which is the number of test cases.
Each test case contains
two lines.
The first line contains an integer, which indicates the number of
puzzle pieces.
The second line contains n different integers, the i-th number
means the mark of the block in the i-th position.
 
Output
For each test case, output one line with one number
represents the minimum operations.
 
Sample Input
2
4
2 3 4 1
4
2 1 4 3
 
Sample Output
3
2

Hint

1<=T<=20,1<=n<=100000

 
 
题意:有顺序不定的从1到n的n个数,排序,每次操作更换两个数的位置,求最少操作次数,使得排序为从变成从 1 到 n。
思路:标记每个数应该所在的位置,然后将该位置上的数寻找其应有的位置并进行标记,并标记好每个数位置是否已经找好。具体看代码注释
 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1e6+10;
int a[N],vis[N]; //vis数组记录每个数是否找过他应该在的位置
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(vis,0,sizeof(vis));
int ans = 0;
int n,i;
scanf("%d",&n);
for(i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
for(i = 1; i <= n; i++){
int temp = a[i],x = 0;
while(!vis[temp]){ //temp 还没找过其应在的位置
vis[temp] = 1; //标记temo已经找过
temp = a[temp]; //用temp应在的位置上的数覆盖temp,如果其在应在的位置那么继续while循环直接跳出然后不用做交换
x++;
}
if(x > 0){
ans += x-1; //遍历每一组情况下应该对x减一才能得到正确的交换次数
}
}
printf("%d\n",ans);
}
return 0;
}

  

hdu6489 2018 黑龙江省大学生程序设计竞赛j题的更多相关文章

  1. 舔狗【2019河北省大学生程序设计竞赛 J题】

    题目描述 > “舔狗舔狗,> 舔到最后,> 一无所有.” 有 n 只舔狗,每只舔狗的心中都有自己朝思暮想的一位. 每个人虽然受到了一万次拒绝,还毅然第一万零一次鼓起勇气. 作为一个不 ...

  2. 2013年山东省第四届ACM大学生程序设计竞赛J题:Contest Print Server

    题目描述     In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source code ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  6. ZZUOJ-1195-OS Job Scheduling(郑州大学第七届ACM大学生程序设计竞赛E题)

    1195: OS Job Scheduling Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 106  Solved: 35 [id=1195&quo ...

  7. angry_birds_again_and_again(2014年山东省第五届ACM大学生程序设计竞赛A题)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2877 题目描述 The problems ca ...

  8. 【湖南师范大学2018年大学生程序设计竞赛新生赛 L】【HDOJ2476】【区间DP】

    https://www.nowcoder.com/acm/contest/127/L L 小小粉刷匠 题目描述 "lalala,我是一个快乐的粉刷匠",小名一边快活地唱着歌,一边开 ...

  9. "字节跳动杯"2018中国大学生程序设计竞赛-女生专场 Solution

    A - 口算训练 题意:询问 $[L, R]$区间内 的所有数的乘积是否是D的倍数 思路:考虑分解质因数 显然,一个数$x > \sqrt{x} 的质因子只有一个$ 那么我们考虑将小于$\sqr ...

随机推荐

  1. MySql分页查询慢|这里告诉你答案

    一.背景 我们在开发的过程中使用分页是不可避免的,通常情况下我们的做法是使用limit加偏移量:select * from table where column=xxx order by xxx li ...

  2. cmake 递归依赖

    现在有3个模块:main.service.base,main依赖service的service.h.service依赖base的base.h,怎么写CMakeList.txt避免main直接耦合bas ...

  3. 半导体知识:蚀刻(Etch)工艺讲解

    本文转载自微信公众号 - 半导体行业观察  , https://mp.weixin.qq.com/s/F3LXiub6n4iYsQDqDH9K_g

  4. 第三周四则运算辅助(CAI)结对项目需求文档

    小学四则运算辅助(CAI) UI需求: 目的:让更对的小学生能学到更多的知识,提高做题的效率. 背景:该系统应用于小学生数学算术题的出题,判断对错以及错题本,该系统为解决家长每天为孩子出题的不便而解决 ...

  5. vue内置的标签(组件)

    component:用于动态组件,查看博文vue学习之组件. <component :is="componentId"></component> trans ...

  6. javaFX的控制台实现

    最近做了个javaFX的工具,想弄个控制台输出信息,准备用TextArea来模拟console,但直接操纵console对象的话不依赖这个项目的地方就无法输出信息到控制台了,至于log,以前弄过一个输 ...

  7. makefile 嵌套

    目录结构: dynamic 和 static 两个目录实现加法功能, 分别生成动态库和静态库,   main.c 主文件链接加法功能,  终端命令执行:   make dynamic=1  则编译动态 ...

  8. Spring Cloud Commons模块

    上一篇介绍了 Spring Cloud Context模块 ,本文介绍SpringCloud的另一个基础模块 SpringCloud Commons模块 .只要在项目的pom文件中引入了spring- ...

  9. 游记-HNOI2019

    Day -1 最后一场考试依旧没有ak(达成成就:\(\mathrm{OI}\) 生涯 AK 次数仅一次) Day 0 听dalao们说现在做题已经没有意义了,不如多口胡几道题,拓展视野 虽然很抗拒但 ...

  10. HTTP协议详解(四)

    接着第三篇继续学习.... 9 Cookie和Session的比较 Cookie和Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决HTTP无状态的问题而所做的努力. S ...