PTA(Advanced Level)1067.Sort with Swap(0, i)
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 (≤105) 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
思路
- 贪心策略:
- 最好的置换结果就是一次就把数字还原到最后对应的位置上去
- 可能出现的结果是0在不断置换的过程中回到了0的位置,但是整个数组仍非有序的,此时0要是和已经归位的数字发生替换那么就会多出步数,此时应该和还没归位的数字换位置
代码
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++) cin >> a[i];
int remain = 0;
for(int i=0;i<n;i++)
if(a[i] != i && a[i] != 0)
remain++;
int k = 1; //表示下一个0该跳转的位置(如果0跳到0上)
int ans = 0;
while(remain > 0)
{
if(a[0] == 0) //0在本位上
{
while(k < n)
{
if(a[k] != k) //找到第一个不在该在的位置上的数字
{
swap(a[0], a[k]);
ans++;
break;
}
k++;
}
}
while(a[0] != 0) //当0不在0号位的时候
{
swap(a[0], a[a[0]]); //将0所在位置上的数和0进行交换
ans++;
remain--;
}
}
cout << ans;
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805403651522560
PTA(Advanced Level)1067.Sort with Swap(0, i)的更多相关文章
- PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...
- 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 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PTA 1067 Sort with Swap(0, i) (25 分)(思维)
传送门:点我 Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasin ...
- PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]
题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...
随机推荐
- centos7下用kubeadm安装k8s集群并使用ipvs做高可用方案
1.准备 1.1系统配置 在安装之前,需要先做如下准备.三台CentOS主机如下: 配置yum源(使用腾讯云的) 替换之前先备份旧配置 mv /etc/yum.repos.d/CentOS-Base. ...
- PKUSC2019划水记
Day -3~Day -2 划了两天的水,考了两次IOI赛制,垫了两次底.... 怕不是要凉啊(安慰自己才C3还有时间) Day -1 坐火车了,差点把身份证弄丢. 睡了一觉,肝了你的名字(结局草率差 ...
- 为什么使用Spring Boot
原文:https://dzone.com/articles/why-springboot 作者:Siva Prasad Reddy Katamreddy 译者:Oopsguy 本文将介绍各种 Spri ...
- arcgis python 删除一个数据库所有数据
# -*- coding: cp936 -*- import xlrd # must init xlrd import arcpy import os def main(): arcpy.env.wo ...
- 解析PHP的self关键字
PHP群里有人询问self关键字的用法,答案是比较明显的:静态成员函数内不能用this调用非成员函数,但可以用self调用静态成员函数/变量/常量:其他成员函数可以用self调用静态成员函数以及非静态 ...
- 慢查询explan详解
慢查询排查 show status; // 查询mysql数据库的一些运行状态 show status like 'uptime'; // 查看mysql数据库启动多 ...
- Ubuntu 18.04系统下arm-linux-gcc交叉编译器安装
Ubuntu 18.04系统: arm-linux-gcc 4.4.3版本. 安装arm-linux-gcc将压缩包arm-linux-gcc.tar.gz解压到arm-linux-gcc文件夹tar ...
- 安装vue模板时,选择webpack-simple还是Webpack?
选用模板常用的是webpack与webpack-simple.webpack-simple是基于Webpack@2.1.0-beta.25进行配置的版本,而webpack模板则是基于Webpack ^ ...
- OOM异常的发生原因
一,jvm内存区域 1,程序计数器 一块很小的内存空间,作用是当前线程所执行的字节码的行号指示器. 2,java栈 与程序计数器一样,java栈(虚拟机栈)也是线程私有的,其生命周期与线程相同.通常存 ...
- ubuntu tensorflow cpu faster-rcnn train data
(flappbird) luo@luo-All-Series:~/MyFile/tf-faster-rcnn_box$ (flappbird) luo@luo-All-Series:~/MyFile/ ...