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. web应急:新闻源网站劫持

    新闻源网站一般权重较高,收录快,能够被搜索引擎优先收录,是黑灰产推广引流的必争之地,很容易成为被攻击的对象.被黑以后主要挂的不良信息内容主要是博彩六合彩等赌博类内容,新闻源网站程序无论是自主开发的还是 ...

  2. kvm虚拟机的克隆

    kvm虚拟机的克隆分为两种情况 (1) KVM主机本机虚拟机直接克隆. (2) 通过复制配置文件与磁盘文件的虚拟机复制克隆(适用于异机的静态迁移). 1.  本机虚拟机直接克隆 (1) 查看虚拟机配置 ...

  3. Java-Queue总结

    1. ConcurrentLinkedQueue 基础链表同步队列. import java.util.Queue; import java.util.concurrent.ConcurrentLin ...

  4. windows系统mysql-5.7官方绿色版zip包安装教程

    准备 下载页面:https://dev.mysql.com/downloads/mysql/ 点击 Download 按钮下载zip包到本地,解压(以我本地的解压路径是 D:\db\mysql-5.7 ...

  5. Oracle的创建表空间及用户

    学习笔记: 1.创建表空间 --创建表空间 create tablespace thepathofgrace datafile 'c:\thepathofgrace.dbf' size 100m au ...

  6. Web API 2 的操作结果

    这是msdn原文文档!明天用,留存. Web API 控制器操作可以返回以下任何内容: void HttpResponseMessage IHttpActionResult 其他类型 根据返回的这种情 ...

  7. PIE SDK归一化水体指数法

    1.算法功能简介 归一化指数法(NDWI(Normalized Difference Water Index,归一化水指数)),用遥感影像的特定波段进行归一化差值处理,以凸显影像中的水体信息. 其表达 ...

  8. Git remote: ERROR: missing Change-Id in commit message

    D:\code\项目仓库目录>git push origin HEAD:refs/for/dev/wangteng/XXXXX key_load_public: invalid format E ...

  9. 面试官问我,为什么老司机建议MySQL列属性尽量用 NOT NULL ?

    本文阅读时间大约6分钟. 其实写这篇文章,也是来自一个知识星球读者的提问,他在二面的过程中被问到了,由于他简历中写道有 MySQL 调优经验,但这个问题没有回答好,二面被刷了. 其实我们刚学习 C 语 ...

  10. python3自动部署mariadb主从

    master import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.rep ...