hdu6489 2018 黑龙江省大学生程序设计竞赛j题
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.
represents the minimum operations.
4
2 3 4 1
4
2 1 4 3
2
1<=T<=20,1<=n<=100000
#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题的更多相关文章
- 舔狗【2019河北省大学生程序设计竞赛 J题】
题目描述 > “舔狗舔狗,> 舔到最后,> 一无所有.” 有 n 只舔狗,每只舔狗的心中都有自己朝思暮想的一位. 每个人虽然受到了一万次拒绝,还毅然第一万零一次鼓起勇气. 作为一个不 ...
- 2013年山东省第四届ACM大学生程序设计竞赛J题:Contest Print Server
题目描述 In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source code ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- 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 ...
- angry_birds_again_and_again(2014年山东省第五届ACM大学生程序设计竞赛A题)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2877 题目描述 The problems ca ...
- 【湖南师范大学2018年大学生程序设计竞赛新生赛 L】【HDOJ2476】【区间DP】
https://www.nowcoder.com/acm/contest/127/L L 小小粉刷匠 题目描述 "lalala,我是一个快乐的粉刷匠",小名一边快活地唱着歌,一边开 ...
- "字节跳动杯"2018中国大学生程序设计竞赛-女生专场 Solution
A - 口算训练 题意:询问 $[L, R]$区间内 的所有数的乘积是否是D的倍数 思路:考虑分解质因数 显然,一个数$x > \sqrt{x} 的质因子只有一个$ 那么我们考虑将小于$\sqr ...
随机推荐
- linux 命令之文件读取,head, tail, tailf, sed
head 看文件的前100行head -100 filename tail/tailf查看文件的后100行tail -100 filename 或 tail -n 100 filename ta ...
- 一些C语言基础知识
位运算 // 按位与&: 2 & 3 = 2: 010 & 011 = 010 两个1才为1 // 按位或|: 2 | 3 = 3: 010 | 011 = 011 只 ...
- robotframework环境安装
1.安装 robotframework 执行命令 pip install robotframework 2.安装seleniumlibrary 执行命令 pip install --upgrade r ...
- Python中所有的关键字
在python中若想查询python中有哪些关键字可以先导入keyword模块 import keyword #导入关键字模块print(keyword.kwlist) #查询所有关键字 查询结果: ...
- python核心编程(多线程编程)
1.全局解释器锁 2.threading模块 thread类
- vue.js学习系列-第二篇
一 VUE实例生命周期钩子 1 生命周期函数 定义 生命周期函数就是vue在某一时间点自动执行的函数 2 具体函数 1 new vue() 2 before ...
- EASY-X
----------------------- Page 1----------------------- 一 创建新项目 VC 写程序要有项目的概念,一个项目可以有多个 .cpp 文件,多个项目构 ...
- 好奇,项目根目录下的.editorconfig文件
一..editorconfig文件是什么? 在项目里,大多时候都能看到.editorconfig文件,刚开始总是忽视掉它,认为它不太重要.但是,它的存在,必定有它的理由,于是,抽空来研究一下,它是什么 ...
- 机器学习基石11-Linear Models for Classification
注: 文章中所有的图片均来自台湾大学林轩田<机器学习基石>课程. 笔记原作者:红色石头 微信公众号:AI有道 上一节课,我们介绍了Logistic Regression问题,建立cross ...
- 爬虫 http原理,梨视频,github登陆实例,requests请求参数小总结
回顾:http协议基于请求响应的方式,请求:请求首行 请求头{'keys':vales} 请求体 :响应:响应首行,响应头{'keys':'vales'},响应体. import socket soc ...