The puzzle:

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6489

找大佬讲了一下这道题,但还是不懂为什么要这样做,先记录一下以后再研究研究;

解题思路:

 这道题是把序列换几次能到增序列,一上来pos数组记录一下每个数都在什么位置,然后就从1开始和第一个数进行匹配(数列是1-n的),如果不相等,那我们只要进行一次交换就可以把i放到这个位置,之前记录了每个数的位置直接进行更改i与pos[i]的位置就可以 然后ans++ 
 
(如果有看到的大佬,欢迎留言教教我QAQ)
 
 
AC代码如下:
 /* */
# include <bits/stdc++.h> using namespace std;
const int N=1e5 + ;
int pos[N]; int main()
{
int t;
scanf("%d", &t);
while( t-- )
{
int n;
scanf("%d", &n);
for( int i=; i<=n; i++ )
{
int x;
scanf("%d", &x);
pos[x] = i;
} int ans = ; for( int i=; i<=n; i++ )
{
while( pos[i]!=i )
{
swap(pos[i], pos[pos[i]] );
ans++;
}
}
printf("%d\n", ans);
}
return ;
}
 

The puzzle的更多相关文章

  1. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...

  2. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

  3. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  4. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  5. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  6. codeforce B Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  8. Ignatius's puzzle

    Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. A hard puzzle

    A hard puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. hdu 1097 A hard puzzle

    Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...

随机推荐

  1. 阿里云最新Maven仓库地址 从此 我的maven依赖下载666~

    配置指南 maven配置指南 打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在<mirrors></mirrors> ...

  2. MongoDB 增删改查 Shell使用及操作

    下载链接:https://robomongo.org/download 安装步骤省略,下一步下一步... 图形界面,连接默认,取个名字就行. 连接成功,可以愉快的使用了,不用总是敲命令了,简洁方便,多 ...

  3. Java调用WebService方法总结(8)--soap.jar调用WebService

    Apache的soap.jar是一种历史很久远的WebService技术,大概是2001年左右的技术,所需soap.jar可以在http://archive.apache.org/dist/ws/so ...

  4. 单例模式的双重锁为什么要加volatile(转)

    单例模式如下: 需要volatile关键字的原因是,在并发情况下,如果没有volatile关键字,在第5行会出现问题. instance = new TestInstance();可以分解为3行伪代码 ...

  5. 使用ngspice进行电路仿真

    电路spice仿真工具已经比较成熟,开源的免费工具也有不错的性能.使用ngspice可以得到不错的仿真结果. 在Linux系统上,例如写一个RLC谐振的电路: RLCV1 1 0 AC 1V L 1 ...

  6. union 和 union all的区别

    union 和 union all的区别 相同点和不同点 相同点:union和union all 都是对于多个查询结果的并集进行操作不同点:1.union 不会输出两个结果并集的重复行2.union ...

  7. python3实现unix域协议(tcp、udp)通讯

    socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIXDomain Socket.虽然网络socket也可用于同一台主机的进程间通讯(通过loopb ...

  8. 【Redis】基本数据类型及命令操作(超详细)

    一.String 1.1 概述 1.2 相关命令列表 1.3 命令示例 二.List 2.1 概述: 2.2 相关命令列表: 2.3 命令示例: 2.4 链表结构的小技巧: 三.Hashes 3.1 ...

  9. PM2 监控 Spring Boot 项目运行

    更多 PM2 的用法介绍请参考: PM2简易使用手册 - 掘金 由于 PM2 通常都用于 node 应用, 所以 exec_mode 应写为 fork, 其中最重要的是 args, -jar 和 ja ...

  10. keepalived,tomcat,memcache

    1.Nginx+Keepalived实现站点高可用 linux cluster类型 LB:nginx负载,varnish(director module)haproxy,lvs HA:keepaliv ...