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 (≤105) 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在不断置换的过程中回到了0的位置,但是整个数组仍非有序的,此时0要是和已经归位的数字发生替换那么就会多出步数,此时应该和还没归位的数字换位置
代码
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++) cin >> a[i];
int remain = 0;
for(int i=0;i<n;i++)
if(a[i] != i && a[i] != 0)
remain++;
int k = 1; //表示下一个0该跳转的位置(如果0跳到0上)
int ans = 0;
while(remain > 0)
{
if(a[0] == 0) //0在本位上
{
while(k < n)
{
if(a[k] != k) //找到第一个不在该在的位置上的数字
{
swap(a[0], a[k]);
ans++;
break;
}
k++;
}
} while(a[0] != 0) //当0不在0号位的时候
{
swap(a[0], a[a[0]]); //将0所在位置上的数和0进行交换
ans++;
remain--;
}
}
cout << ans;
return 0;
}
引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805403651522560

PTA(Advanced Level)1067.Sort with Swap(0, i)的更多相关文章

  1. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)

    只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...

  2. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

  3. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

  4. PAT 1067. Sort with Swap(0,*)

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

  5. 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 ...

  6. PAT 甲级 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 ea ...

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

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given a ...

  8. 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 ...

  9. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

随机推荐

  1. [Luogu] U18202 洞穴遇险

    https://www.luogu.org/problemnew/show/U18202 暴力搜索预期得分3030分左右. 状压预期得分7070分左右. 考虑费用流,将剩余不稳定度和最小转为消除不稳定 ...

  2. [Luogu] 1600

    https://www.luogu.org/problemnew/show/P1600 nlogn竟然T了 #include <iostream> #include <cstdio& ...

  3. Noip2013 提高组 Day2 T1 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...

  4. 随手记录---jq如何判断当前元素是第几个元素

    主要自己总是不记得 结构如下,涉及jq中获取当前元素是父元素的的第几个元素,jq中获取某类在同类元素中占第几,each方法 <div class="parent"> & ...

  5. AE开发之shp转txt

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. JavaWeb_(Spring框架)Spring中IoC与DI概念入门

    Spring是于2003 年兴起的一个轻量级的Java 开源框架,它由Rod Johnson创建.传统J2EE应用的开发效率低,Spring作为开源的中间件,提供J2EE应用的各层的解决方案,Spri ...

  7. Python学习日记(三)——Python基本数据类型(运算符、int、str、tuple、dict、range)

    运算符 1.算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2 ...

  8. Java并发指南13:Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析

    Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 转自https://www.javadoop.com/post/hashmap#toc7 部分内容转自 http: ...

  9. pwn学习日记Day20 《程序员的自我修养》读书笔记

    可执行文件的装载与进程 覆盖装入和页映射是两种典型的动态装载方法 进程建立的三步 1.创建一个独立的虚拟地址空间 2.读取可执行文件头,并且建立虚拟空间与可执行文件的映射关系. 3.将CPU的指令寄存 ...

  10. insmod mknod

    insmod module 會在 /proc/devices/ 下產生 major number 及 device name ---------------------------- mknod 會使 ...