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
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ; int main()
{
int n;
int num; //数组存储方式调换,便于交换arr[0]和arr[arr[0]]
int arr[maxn] = {};
scanf("%d",&n); int left = n - ; //控制循环次数,需要交换数的个数,0除外
for (int i = ; i < n; i++)
{
scanf("%d",&num);
arr[num] = i;
if (num == i && num != )
{
left--;
}
} int k = ; //如果排序还未最终完成但是 0 已经回到arr[0]处,交换arr[k]值
int ans = ; //纪录交换的次数
while (left > )
{
if (arr[] == )
{
while (k < n)
{
if (arr[k] != k)
{
swap(arr[], arr[k]);
ans++;
break;
}
k++;
}
}
else
{
swap(arr[], arr[arr[]]);
ans++;
left--;
}
} printf("%d", ans);
return ;
}

10-排序6 Sort with Swap(0, i) (25 分)的更多相关文章

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

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

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

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

  5. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

  6. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

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

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

  8. PAT_A1067#Sort with Swap(0, i)

    Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...

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

随机推荐

  1. Java解压和压缩带密码的zip或rar文件(下载压缩文件中的选中文件、向压缩文件中新增、删除文件)

    JAVA 实现在线浏览管理zip和rar的工具类 (有密码及无密码的)以及下载压缩文件中的选中文件(向压缩文件中新增.删除文件) 这是之前的版本 JAVA 解压压缩包中指定文件或实现压缩文件的预览及下 ...

  2. Jmeter参数化之数据库读取数据

    以读取mysql数据库为例 1.下载一个mysql驱动包,最好去mysql官网下载 下载网址:https://dev.mysql.com/downloads/connector/j/ Select O ...

  3. 【linux】linux磁盘空间 目录查看清理 和 文件查看清理

    =========================大目录排查============================= 一.首先查看磁盘挂载,磁盘空间使用情况 1.进入根路径 cd / 2.查看磁盘挂 ...

  4. Application类-多窗口交互

    我们在派生自Application类中出来放置响应应用程序事件的代码外,还可以放置一些完成其他任务的代码. 在此之前要知道: 如何获取应用程序的Application对象: //App是一个继承自Ap ...

  5. 初识AspNet Core中的标识Identity

    AspNet Core中的标识Identity,是用于Web应用程序的成员身份验证系统. 最方便的引入办法是在创建MVC或Pages的Web应用时,直接选择相应的身份验证系统.如图: 如果选择的是“个 ...

  6. 如何提高sql查询性能到达优化程序的目的

    1.关于SQL查询效率,100w数据 SQL查询效率 step by step -- setp 1.-- 建表create table t_userinfo(userid int identity(1 ...

  7. 如何禁用/关闭vs2017自带的Git工具的?

    对于用习惯了独立Git工具和命令行的人来说,看到Visual Studio自带的Git工具后,很是别扭,到处充满了不习惯,而且是不是还会出现电脑卡顿的现象(可能是我自身电脑配置一般的问题). 如何关闭 ...

  8. Java自学-I/O 字符流

    Java的字符流 Reader Writer Reader字符输入流 Writer字符输出流 专门用于字符的形式读取和写入数据 步骤 1 : 使用字符流读取文件 FileReader 是Reader子 ...

  9. Redis缓存数据库基础

    思维导图xmind文件:https://files-cdn.cnblogs.com/files/benjieming/Redis.zip

  10. mybatis源码/mybatis执行流程源码解析

    https://www.cnblogs.com/cxiaocai/tag/%E9%9D%A2%E8%AF%95%E9%A2%98/public SqlSession session; public S ...