1067 Sort with Swap(0, i) (25 分)

Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}

Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (≤10​5​​) followed by a permutation sequence of {0, 1, ..., N−1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:

10
3 5 7 2 6 4 9 0 8 1

Sample Output:

9

经过演算发现有 0 的环是 n-1 没0的环是n+1当然了 个数要大于等于2才行

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int digit[maxn], pre[maxn];
int cnt[maxn];
int find(int x){if(x == pre[x])return x;return pre[x] = find(pre[x]);
}
void merge(int x, int y)
{int fx = find(x);int fy = find(y);if(fx != fy){pre[fx] = fy;}
}
int main()
{ios::sync_with_stdio(false);int n;cin>>n;for(int i = 0; i < n ; i++){cin>>digit[i];  pre[i] = i;}   for(int i = 0 ; i < n; i ++)merge(i , digit[i]);for(int i = 0; i < n; i ++)cnt[find(i)] ++;   int gg = cnt[find(0)];int kk = find(0);int ans = 0;if(gg != 1) //表明不是digit[0] = 0 ans = gg - 1; for(int i = 0 ; i < n ; i++){if(cnt[i] == 1 || i == kk || cnt[i] == 0)continue;ans = ans + cnt[i] + 1; } cout<<ans<<endl;return 0;
}

1067 Sort with Swap(0, i) (25 分)思路相关推荐

  1. 【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any permutation of the numbers {0, 1, 2,-, N−1}, it is easy ...

  2. 1067 Sort with Swap(0, i) (25 分)【难度: 中 / 知识点: 置换群】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805403651522560 这种相关的题目见过很多次了. 常见的是只可以 ...

  3. 10-排序6 Sort with Swap(0, i) (25 分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  4. 1067 Sort with Swap(0, i) (25 分)

    1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...

  5. PAT甲级1067 Sort with Swap(0, i):[C++题解]此题不是很懂!!

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:y总从图论的角度来讲解的这道题,听得不是很懂. 此题不是很懂,暂留以后探讨.存在鸽的可能. ac代码 #include<bits ...

  6. 1067. Sort with Swap(0,*)

    好弱啊,这个题目折腾了好久. 构造hash表用来维护各个元素所在的位置,利用map维护一颗红黑树来保存还未确定的元素的位置. (1)用0不断的跟其他元素交换,每次交换都会保证一个元素在正确的位置. ( ...

  7. PTA 1067 Sort with Swap(0, i) (25 分)(思维)

    传送门:点我 Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasin ...

  8. PAT A1067 Sort with Swap(0, i) ——天街小雨润如酥,草色遥看近却无

    PAT A1067 Sort with Swap(0, i) 本题使用了姥姥教的方法,通过交换过程(第一个开始动的元素,通过一系列交换到达自己应该在的位置)可以发现他们形成了一个闭环,大家手拉手,每个 ...

  9. 算法 排序6 Sort with Swap(0, i) 2013年免试研究生上机考试真题

    全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案 题目:Given any permutation of the numbers {0, 1, 2,..., N−1}, ...

  10. 7-16 Sort with Swap(0, i) | PTA数据结构与算法——C语言实现

    2013年浙江大学免试研究生上机考试真题. 原题链接:PTA | 程序设计类实验辅助教学平台 题目描述 给定包含数字 {0, 1, 2,..., N−1} 的任一排列,很容易对它们进行升序排序. 但是 ...

最新文章

  1. Linux开启路由转发功能(透明代理环境搭建)
  2. awk 内嵌正则 提取字符串_干货-Shell编程文本处理三剑客之-awk
  3. 软件工程--第五章-- 总体设计
  4. c语言中如何取消最后一个空格,新人提问:如何将输出时每行最后一个空格删除...
  5. Hibernate映射关系之多对多
  6. php pathinfo()函数
  7. QtCreator导入yocto sdk记录
  8. Industrial Design System v4.5 1CD(工业产品设计软件)
  9. Error-Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to
  10. window.dialogArguments.location.reload();
  11. Free Pascal初次体验(有亮点哦)
  12. matlab电磁场与微波技术相关仿真的代码,几乎覆盖电磁和微波领域
  13. youtube的使用体会
  14. UPC6605 所罗门王的宝藏
  15. 将你的笔记本变成无线路由器
  16. 汽车学堂 自 动 驾 驶 决 策 与 控 制 算 法——现 代 控 制 理 论学习(一)
  17. 实时级嵌入式系统半实物仿真测试平台系统描述
  18. 图基准数据集(OGB)
  19. vue2进阶篇:vue-router之“使用独享路由守卫”
  20. IOS keychain 使用

热门文章

  1. [Linux] 通过shell给unix socket发送数据
  2. 给服务器端DropDownList控件添加客户端onchange事件
  3. Python文档学习笔记(1)--使用Python 解释器
  4. linux /dev/null 中有数据
  5. IBM PowerAI编程大赛Q2场来袭!探秘金融语料大数据识别
  6. dubbo学习--源码结构概述(5)
  7. HTML+JS实现可编辑表格
  8. 跟我一起数据挖掘(19)——什么是数据挖掘(2)
  9. 爱不意味这“sorry”
  10. 【老孙随笔】想学程序设计,先学人生设计!