原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/678

题目如下:

Given any permutation of the numbers {0, 1, 2,..., N−1N-1N−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 NNN nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive NNN (≤105\le 10^5≤10​5​​) followed by a permutation sequence of {0, 1, ..., N−1N-1N−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应该和它所在位置的元素进行交换……之后参考了陈越老师的讲解,她的思路是得利用在讲“表排序”
中的一个结论,即“N个数字的排列是有若干个独立的环组成”,并结合题目要求,则元素“0”相当于表排序中的“空位”,因此可以进行如下讨论: 
  (1)如果环是单环,那么表示该元素已经在其正确的位置,无需排序;
  (2)如果环是多环(环的元素个数为N0)且包含元素0,0相当于空位,那么每次将0和该位置本来有的元素经行交换,是原来0在的位置上填上正确的元素,那么经过N0-1次交换,即可完成该环内的正
确排序;
  (3)如果环为多环(环的元素的个数为Ni)且不包含0,那么先让0和环内任一元素进行交换将0换进来,此时利用(2)中的结论,改环需要进行(Ni+1)-1次交换即可完成环内的正确排序,但由于把
0换进该环多一次交换,故总的交换次数为Ni+1。
  因此,当待排序列的第一个数不为0时,则该序列可分为以上三种环,且(2)环的个数只能为1。设有S个单环,K个多环,则总的交换次数为(N0-1)+(Ni+1)(i从1到K-1),即Ni+K-2(i从1到K),
又Ni(i从1到K)等于N-S,故总的交换次数为:N-S+K-2;
  但是,当待排序列的第一个数为0时,那么(2)环的个数为0,(3)环的个数即为K,因此该公式修改为Ni+1(i从1到K),即N-S+K;
  除此之外,还应该考虑到待排序列已经正确(包括S==N 和N==1)两种情况,此时输出为0。
 #include<stdio.h>
#define Max 100000 int main()
{
int A[Max],Table[Max],flag[Max],N;
int i,tmp,S,K;
S=K=;
scanf("%d",&N);
for (i=;i<N;i++)
{
scanf("%d",&A[i]);
flag[i]=; //标识元素访问过了没有
}
/* 指针数组,用来存放正确的序号 */
for (i=;i<N;i++)
{
Table[A[i]]=i; //即元素A[i]存放在序号i中
} for (i=;i<N;i++)
{
if (flag[i]==)
{
if (Table[i]!=i)
{
flag[i]=;
tmp=Table[i];
while(flag[tmp]==)
{
flag[tmp]=;
tmp=Table[tmp];
}
K++;
}
else if (Table[i]==i)
{
flag[i]=;
S++;
}
}
}
if (A[]==)printf("%d",N-S+K);
else if (S==N)printf("");
else printf("%d",N-S+K-);
return ;
}
 

Sort with Swap(0, i)的更多相关文章

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

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

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

  5. pat1067. Sort with Swap(0,*) (25)

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

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

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

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

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

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

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

随机推荐

  1. CocoaPods报错:The dependency `AFNetworking ` is not used in any concrete target 解决办法

    产生这个问题的原因是最新版本的CocoaPods把Podfile文件的书写格式改变了,官网推荐用如下格式书写: platform :ios, '8.0' use_frameworks! target ...

  2. LocalDB连接失败

    最近项目尝试放弃sqlexpress2005使用LocalDb2014,遇到问题: LocalDB安装成功,能正常添加实例,但同一个winform程序 在不同的win7电脑上有的能连上,有的怎么也连接 ...

  3. Git 常用命令详解

    Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如:linux kernel),管理私人的文档和源代码也有很多优势(如:wsi-lgame-pro) Git 的更多介绍 ...

  4. Linux下文件的三种时间戳

    Linux下文件的三种时间标记 三种时间对应关系表 column column column 访问时间 Access atime 修改时间 Modify mtime 状态改动时间 Change cti ...

  5. Storm Windowing storm滑动窗口简介

    Storm Windowing 简介 Storm可同时处理窗口内的所有tuple.窗口可以从时间或数量上来划分,由如下两个因素决定: 窗口的长度,可以是时间间隔或Tuple数量: 滑动间隔(slidi ...

  6. /usr/bin/ld.bfd.real: cannot find -lGL /usr/bin/ld.bfd.real: cannot find -lX11

    /usr/bin/ld.bfd.real: cannot find -lGL /usr/bin/ld.bfd.real: cannot find -lX11 根据网上大多数的说法,以及官网的介绍.截至 ...

  7. Linux内核:kthread_create(线程)、SLEEP_MILLI_SEC

    转自:http://blog.csdn.net/guowenyan001/article/details/39230181 一.代码 #include <linux/module.h> # ...

  8. RSA密钥之C#格式与Java格式转换

    前言 最近由于项目需求,服务端由c#编写,客户端由java编写.通信数据使用RSA非对称加密.但是java和c#生成的密钥格式是不一样的,所以需要转换格式才可以正常使用.网上搜到使用java进行格式转 ...

  9. 隐藏进程中的模块绕过IceSword的检测

    标 题: [原创] 隐藏进程中的模块绕过IceSword的检测 作 者: xPLK 时 间: 2008-06-19,17:59:11 链 接: http://bbs.pediy.com/showthr ...

  10. distinct 与 group by 去重

    例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Nam ...