PAT_A1067#Sort with Swap(0, i)
Source:
Description:
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 (≤) 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
Keys:
Code:
/*
Data: 2019-07-22 19:54:12
Problem: PAT_A1067#Sort with Swap(0, i)
AC: 29:14 题目大意:
排序,只能交换0和任意其他数,给出需要的最少步数 基本思路:
1.0在几号位就跟相应的数字换位置,每次操作可以将一个数字归位;
2.若0回到了0号位,则再与任意一个不在自己位置上的数字交换;
3.重复第一步,直至所有数字都归位;
*/ #include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e5+;
int pos[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,num,left=,cnt=,st=;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d", &num);
pos[num]=i;
if(num!=i && num!=)
left++;
}
while(left!=)
{
if(pos[]==){
for(int i=st; i<n; i++){
if(pos[i]!=i){
swap(pos[],pos[i]);
st=i;break;
}
}
cnt++;
}
swap(pos[],pos[pos[]]);
cnt++;
left--;
}
printf("%d", cnt); return ;
}
PAT_A1067#Sort with Swap(0, i)的更多相关文章
- 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 ...
- Pat1067:Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
- PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...
- pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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-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 ...
- 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 ...
- 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 ...
随机推荐
- ceph-报错日志
由于时钟不一致问题,导致ceph存储有问题 clock skew时钟偏移overalladj. 全部的:全体的:一切在内的stampedadj. 铭刻的:盖上邮戳的:顿足的 beaconvt. 照亮, ...
- bp网络全解读
https://blog.csdn.net/weixin_40432828/article/details/82192709
- vim 更改注释颜色
在 ~/.vimrc 添加命令: highlight Comment ctermfg=green
- nginx logformat说明
记录一下nginx logformat的相关说明 log_format格式变量:$remote_addr #记录访问网站的客户端地址$remote_user #远程客户端用户名$time_loca ...
- ng -v 不是内部或外部命令
输入 npm install -g @angular/cli ng new my-app 提示ng -v 不是内部或外部命令 解决方法 1.安装过程中是否出错 原因可能是npm install -g ...
- 25. SPI
- Tika教程
Apache Tika 是什么? Apache Tika用于文件类型检测和从各种格式的文件内容提取的库. 在内部,Tika使用现有的各种文件解析器和文档类型的检测技术来检测和提取数据. 使用Tika, ...
- [Java Performance] 线程及同步的性能之线程池/ThreadPoolExecutors/ForkJoinPool
线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理 ...
- es+mongodb 整合
之前公司项目的数据都是从mysql查询,后面需求变更:同时技术上相应的也要改变策略,决定将mongodb和mysql的数据通过es建立索引来查询: 对于还没有接触或者真正了解es的可以先看一下相关Lu ...
- ubuntu安装goland
安装goland 首先下载goland https://www.jetbrains.com/zh/go/specials/go/go.html?utm_source=baidu&utm_med ...