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 ...
随机推荐
- java BufferSegment
package org.rx.util; import java.util.function.Consumer; import static org.rx.core.Contract.require; ...
- 建站手册-浏览器信息:Netscape 浏览器
ylbtech-建站手册-浏览器信息:Netscape 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_netscape.asp ...
- MySQL高级学习笔记(六):MySql锁机制
文章目录 概述 定义 生活购物 锁的分类 从对数据操作的类型(读\写)分 从对数据操作的粒度分 三锁 表锁(偏读) 特点 案例分析 建表SQL 加读锁 加写锁 结论 如何分析表锁定 行锁(偏写) 特点 ...
- CS如何调用JS
this.ClientScript.RegisterStartupScript(this.GetType(), "", "RunClick()", true); ...
- js基本函数和基本方法
日期时间函数(需要用变量调用): var b = new Date(); //获取当前时间 b.getTime() //获取时间戳 b.getFullYear() //获取年份 b.getMonth( ...
- Redis源码编译安装
介绍 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写.遵 ...
- Java转型大数据开发全套教程,都在这儿!
众所周知,很多语言技术已经在长久的历史发展中掩埋,这期间不同的程序员也走出的自己的发展道路. 有的去了解新的发展趋势的语言,了解新的技术,利用自己原先的思维顺利改变自己的title. 比如我自己,也都 ...
- python- 属性 静态方法,类方法
一,面向对象结构与成员 1,1 面向对象结构分析: 那么每个大区域又可以分为多个小部分: class A: company_name = '老男孩教育' # 静态变量(静态字段) __iphone = ...
- Mysq sql语句教程
mysql管理命令 show databases; 显示服务器上当前所有的数据库 use 数据库名称; 进入指定的数据库 show tables; 显示当前数据库中所有的数据表 d ...
- 第二十五天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(四)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zwszws/article/details/28493209 6月4日.晴天. ...