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. Elasticsearch:Index alias

    现在让我们来谈谈Elasticsearch最简单和最有用的功能之一:别名 (alias).为了区分这里alias和文章"Elasticsearch : alias数据类型",这里的 ...

  2. ingress-nginx 的使用 =》 部署在 Kubernetes 集群中的应用暴露给外部的用户使用

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247488189&idx=1&sn=8175f067 ...

  3. Elasticsearch方案选型必须了解的10件事!

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484372&idx=1&sn=e863e46 ...

  4. Mapping

    dynamic针对的是新增的字段,不是对mapping中已有的字段 (原有mapping中的字段不受影响,只影响新增的字段) 当dynamic被设置成false的时候,存在新增字段可以被写入到索引文件 ...

  5. mongodb集群搭建(分片+副本)开启安全认证

    关于安全认证得总结: 这个讲述的步骤也是先创建超管用户,关闭服务,然后生成密钥文件,开启安全认证,启动服务 相关概念 先来看一张图: 从图中可以看到有四个组件:mongos.config server ...

  6. Alertmanager结合Slack使用

    Slack作为一款即时通讯工具,协作沟通主要通过Channel(平台)来完成,用户可以在企业中根据用途添加多个Channel,并且通过Channel来集成各种第三方工具. 例如,我们可以为监控建立一个 ...

  7. vue中修改滚动条样式

    这是一个在VUE中的设置滚动条样式的方法,该方法只适用于Chrome浏览器,亲测在火狐不适用 样式写在 APP.vue 中,可以用在全局,如果只用在那个盒子中,只需要在::前面加上盒子的class名就 ...

  8. frp服务利用云主机docker服务实现Windows远程连接

    1.云主机配置 1.docker部署 # 创建文件 mkdir -p /root/docker/frp && touch /root/docker/frp/frps.ini # 配置文 ...

  9. Django 出现 frame because it set X-Frame-Options to deny 错误

    一.背景 使用django3 进行开发时,由于项目前端页面使用iframe框架,浏览器错误提示信息如下 Refused to display 'http://127.0.0.1:8000/' in a ...

  10. NOI2015 洛谷P1955 程序自动分析(并查集+离散化)

    这可能是我目前做过的最简单的一道noi题目了...... 先对e=1的处理,用并查集:再对e=0查询,如果这两个在同一集合中,则为""NO",最后都满足的话输出" ...