题目连接: The puzzle

题目:

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

大概翻译一下: 给你一共1到n, 共n个数字, 而你需要把他们排序, 排序的最终结果是下标 i上存放的数字为 i.
排序只能通过两两交换来改变下标为i处的值, 问你至少需要多少次交换

解题思路:

其实细想可以分为以下三类:

  1. 第一类: 本身就在正确的位置上, 不需要操作.
  2. 第二类: 本应在i位置上的数字在j位置上,而 j位置上的数字恰好在i位置上, 刚好相反, 直接交换, 这样i和j位置都符合要求
  3. 第三类: 本应在i位置上的数字在j位置上,而j位置上的数字在k位置上…

但是如果综合思考, 第二类和第三类是同一类, 因为他们都是需要交换的一类, i位置放的数字不是i, 一定要把i这个数字换到i上, 所以这一步是无法再简化的一步, 第二类情况只不过是第三类情况的一个特例, 可以少进行一次交换.
如果这样来看, 但凡i位置上放的不是i, 那交换次数就要加一.

AC代码:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#define ll long long
using namespace std;
#define MAXN 100005
int a[MAXN]; //a数组是记录当前位置i处的值a[i]
int b[MAXN]; //b数组是记录值i在a数组所处的下标b[i]
int main(void)
{int t; cin >> t;while (t--) {int n; scanf("%d", &n);int res = 0; //存放结果for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);b[a[i]] = i;}for (int i = 1; i <= n; i++) {if (a[i] == i) continue; //符合条件, 不用处理int index = b[i]; //a[i]应放的值i在a[index]处, 需要交换b[a[i]] = index; b[i] = i;swap(a[i], a[index]); res++;}cout << res << endl;}return 0;
}

这个题想清楚思路的话实现起来其实并不会很难, 如果index部分有些不明白不妨代入假想数据去试一试, 尝试理解.

END

The puzzle相关推荐

  1. 学会在Unity中创建一个Match-3益智游戏 Learn To Create a Match-3 Puzzle Game in Unity

    MP4 |视频:h264,1280×720 |音频:AAC,44.1 KHz,2 Ch 语言:英语+中英文字幕(根据原英文字幕机译更准确) |时长:48场讲座(6h 38m) |大小解压后:2.8 G ...

  2. 【杭电ACM】1097 A hard puzzle

    [杭电ACM]1097  A hard puzzle http://acm.hdu.edu.cn/showproblem.php?pid=1097 先用int手写了算法结果竟然wrong answer ...

  3. Eight puzzle --HOJ 11918

    1.题目类型:模拟.哈希表.BFS. 2.解题思路:(1)模拟Eigh Puzzle的变换方式,并记录在数组中 :(2)由于变换的最终结果相同,所以采用反向的BFS遍历所有情况,并记录所有情况:(3) ...

  4. 补第四周作业总结——8 puzzle

    8 puzzle已经提供了解决思路,早期的人工智能算法A.我只能感觉它的神奇,但是没法创造性地使用它.只能按部就班地完成这周的作业. 难点在于对过程的不理解.这个33的格子搜索算法没有尽头,随着步数的 ...

  5. UVA227 Puzzle

    问题链接:UVA227 Puzzle.基础训练级的问题,用C语言编写程序. 问题简述:一个5×5的网格,一个格子是空的,其他格子各有一个字母,一共有四种指令:A,B,L,R,分别表示把空格上.下.左. ...

  6. UVa10639 Square Puzzle(WA)

    例子通过了,并且udebug上的例子也通过了,但是提交还是错误. 针对特殊情况: 3 4 7 0 2 1 2 2 3 3 2 4 2 4 4 0 4 7 0 0 4 0 4 2 3 2 2 1 1 2 ...

  7. 【37.50%】【codeforces 745B】Hongcow Solves A Puzzle

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 如何更好的解决问题 : The puzzle of die

    人们常说,没有学不好的学生,只有不会教的老师.此言差矣,不同学生的智力不同,成长环境不同,自然会在学力的表现上参差不齐.然而好的教育确实又可以在一定层面上改善这种情况.即,尽量让问题满足大部分人的 m ...

  9. c++扔鸡蛋问题egg dropping puzzle(附完整源码)

    C++扔鸡蛋问题egg dropping puzzle 扔鸡蛋问题egg dropping puzzle算法的完整源码(定义,实现,main函数测试) 扔鸡蛋问题egg dropping puzzle ...

  10. leetcode 1178. Number of Valid Words for Each Puzzle | 1178. 猜字谜(bitmask位运算)

    题目 https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/ 题解 看了答案,堪称力扣最详细的答案,从时间复杂度的角度 ...

最新文章

  1. C语言基础(12)-输入和输出
  2. office技巧:Word如何快速的创建表格
  3. Ehab and the Expected XOR Problem
  4. 我用Python分析了“青你2”漂亮小姐姐的颜值,结果真香了!
  5. mescroll上拉加载的实现
  6. Monkey脚本编写
  7. [原创]java WEB学习笔记71:Struts2 学习之路-- struts2常见的内建验证程序及注意点,短路验证,非字段验证,错误消息的重用...
  8. 基于springcloud的声明式客户端FeignClients方式的httpserver访问配置
  9. iOS音频播放 (一):概述 转
  10. 收藏 | 如何撰写好一篇论文?密歇根教授《撰写高影响力论文指南》
  11. Gstreamer学习笔记(5):GStreamer Pad and Capabilities Negotiation
  12. base64和base32的区分方法
  13. 安装ubuntu系统中磁盘分区
  14. 163-H桥电机驱动电路
  15. Java 开发环境搭建
  16. 读经典《C程序设计语言》(The C Programming Language)
  17. oracle 截取小数点_数据库截取小数点后两位小数
  18. PAT 乙级练习 1069 微博转发抽奖
  19. 【英语写作】中国美食文化英语精翻练习
  20. 大学生创业交流会计算机二级,我校举行第七届中国国际“互联网+”大学生创新创业大赛交流研讨会...

热门文章

  1. 什么是软件,软件有哪些特性?
  2. 奶牛戴上VR眼镜“看片”,开心了可以多产奶:俄罗斯官方做了实验,拯救奶牛的冬季忧郁...
  3. 2018宝鸡市高三数学第一次质量检测
  4. 翟菜花:搭上营销快通车的乳业,又是如何玩转互联网营销时代的?
  5. java规则计算_亲属计算规则算法--java实现(关键算法摘要)
  6. 关于virtualbox无法复制粘贴的问题
  7. python_判断是否回文
  8. web应用基本框架图
  9. Centos 7 内网安装 DzzOffice
  10. [计算机网络]网络层IP协议 选路算法