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. 额。。。c++ sort()排序问题

    首先呢 记得 这是个快排 不稳定 基本格式 头文件 #include<algorithm> #include<iostream> bool cmp(int x,int y) { ...

  2. 3.MVC基础-Code First 入门完整实例

    1.添加一个EF的上下文类  EFDbContext public class EFDbContext:DbContext { public EFDbContext() : base("EF ...

  3. Flutter 实现图片裁剪

    实现原理很简单 ,自己绘制一个裁剪框, 根据手势 选择到适合的位置 ,然后将选中的区域绘制到一个新的图片上,从而完成裁剪 裁剪框的绘制  这里我是根据点来连线的  因为每个点上会绘制一个拉伸的标识符 ...

  4. ArcEngine二次开发中运行出现There is no Spatial Analyst license currently available or enabled.

    只需要在许可控件上勾选空间分析功能即可.

  5. ScheduledExecutorService周期性的定时任务

    从j2se的api文档上查看ScheduledExecutorService的方法都是推迟一段时间然后相隔一段时间之后再去执行,没有想Timer定时器一样的可以在定点时间执行的api,如果也想像Tim ...

  6. 【CMDB】高级配置

    思路: 一份默认配置,一份用户自定义配置,将两者结合,自定义优先级大于默认配置 目录结构 ''' 用户自定义的配置文件,settings.py ''' USER = 'xiaoming' PASSWO ...

  7. git注册和基本命令

    注册账户以及创建仓库 要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之 ...

  8. php图形图像处理技术

    图形图像处理技术,gd库的强大支持,PHP的图像可以是PHP的强项,PHP图形化类库,jpgraph是一款非常好用的强大的图形处理工具. 在PHP中加载GD库 gd官方网址下载: http://www ...

  9. python面试总结4(算法与内置数据结构)

    算法与内置数据结构 常用算法和数据结构 sorted dict/list/set/tuple 分析时间/空间复杂度 实现常见数据结构和算法 数据结构/算法 语言内置 内置库 线性结构 list(列表) ...

  10. Shodan information gathering use parameter

    1.we sue the Search Engines Shodan we can gather much information on the line web , such we want to ...