10-排序6 Sort with Swap(0, i) (25point(s))

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

N 个数字的排列由若干个独立的环构成。每一次都是和 0 做 swap,如果这个环里面有 0,长度是 count,那么每次都找和 0 值这个元素的下标相同的元素,做一次 swap。对于这个环本来就有 0,只需要做 count - 1 次 swap。若这个环里面没有 0,就要把 0 先 swap 进去,然后再归位所以比环里有 0 的多 2 次,需要 count + 1 次 swap。如果 count == 0 那这个元素不用和谁做 swap。

#include <stdio.h>

#define N 100000
/* 每次都和 0 做 swap,一次归位一个数字 */
int main(int argc, char const *argv[])
{
int a[N];
int visited[N] = {0};
int n, i; scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
} int sum = 0; /* 总共需要多少次 swap */
for (i = 0; i < n; i++) {
if (!visited[i]) {
visited[i] = 1;
int index = i;
int count = 0; /* 环的元素个数,0 个不用做 swap */
int next;
while (a[index] != index) {
visited[index] = 1;
count++;
next = a[index];
a[index] = index;
index = next;
}
if (count != 0) {
sum = index == 0 ? sum + count - 1 : sum + count + 1;
}
// printf("index = %d, count = %d\n", index, count);
}
}
printf("%d\n", sum);
return 0;
}

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

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

  2. Pat1067:Sort with Swap(0,*)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

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

  4. 7-16 Sort with Swap(0, i)(25 分)

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

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

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

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

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

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

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

  9. PAT1067. Sort with Swap(0, *) (25) 并查集

    PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...

随机推荐

  1. 新版本中的hits.total匹配数说明

    在7.0版发布之前,hits.total始终用于表示符合查询条件的文档的实际数量.在Elasticsearch 7.0版中,如果匹配数大于10,000,则不会计算hits.total. 这是为了避免为 ...

  2. java基础-冒泡排序以及稀疏数组

    java基础 以下内容为本人的学习笔记,如需要转载,请声明原文链接   https://www.cnblogs.com/lyh1024/p/16720908.html Ø 冒泡排序原理: 比较数组中, ...

  3. CentOS 7.9 安装 kafka_2.13

    一.CentOS 7.9 安装 kafka_2.13 地址 https://kafka.apache.org/downloads.html 二.安装准备 1 安装JDK 在安装kafka之前必须先安装 ...

  4. 路径分析—PostgreSQL+GeoServer+Openlayers(二)

    路径分析-QGIS+PostgreSQL+PostGIS+pgRouting(一) 路径分析-PostgreSQL+GeoServer+Openlayers(二) 前言 上一篇文章中实现数据库层面的路 ...

  5. CRT & EXCRT 学习笔记

    这玩意解决的是把同余方程组合并的问题. CRT的核心思想和拉格朗日插值差不多,就是构造一组\(R_i\)使得$\forall i,j(i \neq j) $ \[R_im_i = 1, R_im_j ...

  6. 驱动开发:内核特征码扫描PE代码段

    在笔者上一篇文章<驱动开发:内核特征码搜索函数封装>中为了定位特征的方便我们封装实现了一个可以传入数组实现的SearchSpecialCode定位函数,该定位函数其实还不能算的上简单,本章 ...

  7. SpringBoot 2.5.5整合轻量级的分布式日志标记追踪神器TLog

    TLog能解决什么痛点 随着微服务盛行,很多公司都把系统按照业务边界拆成了很多微服务,在排错查日志的时候.因为业务链路贯穿着很多微服务节点,导致定位某个请求的日志以及上下游业务的日志会变得有些困难. ...

  8. 齐博x1工单碎片模板制作教程

    可以把工单插入到任何频道的内容里边,如下图所示 碎片模板制作标准如下 <form action="{:urls('order/add')}" class="wn_f ...

  9. 小米MIUI禁止系统更新

    删除downloaded_rom的文件夹,随便找一个文件(文件,不是文件夹),重名为downloaded_rom(是把一个文件重命名),这样系统后台偷偷下载时,就不知道该存放更新包的文件,就无法偷偷更 ...

  10. 3.pygame快速入门-游戏循环及动画实现

    游戏循环的开始,意味着游戏的正式开始,游戏循环的作用如下 1.保证游戏不会直接退出 2.变化图像的位置--动画效果 3.检测用户交互--按键.鼠标等     游戏时钟 pyagame提供了一个pyga ...