传送门:点我

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

题目大意:给定n个数字(1到n),且每次只能跟0交换,问换成递增的最少步数。
思路:
核心是让每次交换尽量都把0和当前0这个位置的数组下标,这两个数换位置,这样就不浪费这一次交换。
记录下每个元素所在的位置存入数组pos,即pos数组存放的是pos[number]=ard,表示Number这个数字当前在ard这个位置
如果0不在第0个位置,那么每次可以交换0位置和0所在位置在递增之后应该有的元素。
比如说4 0 3 1 2。先把0和pos[0],即0和1交换,这样1就在它本来应该在的位置。数组变成了4 1 3 0 2,再交换,直到0回到了下标0这个位置。
这样一趟下来保证了每次操作都是有意义的,即每次交换都能把一个元素放回它应该在的位置。
如果这个元素这样交换下来,还没在它应该在的位置,即pos[number]!=number,那就让它去0呆着,下次交换会把它放回应该属于它的位置的。
参考博客:点我 代码:
#include<bits/stdc++.h>
using namespace std;
int pos[];
int main()
{
int n;
scanf("%d",&n);
for(int i = ; i < n ; i ++){
int x;
scanf("%d",&x);
pos[x] = i;
}
int cnt = ;
for(int i = ; i < n ; i ++){
if(pos[i] != i){
while(pos[] != ){
swap(pos[],pos[pos[]]);cnt++;
}//如果0不在数组下标为0这个位置,那可以不停的交换0所在位置数组下标 和 0这两个元素。
if(pos[i] != i){
swap(pos[],pos[i]);
cnt++;
}
}
}
printf("%d\n",cnt);
}
 

PTA 1067 Sort with Swap(0, i) (25 分)(思维)的更多相关文章

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

  2. 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 increasing order ...

  3. 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 increasing order ...

  4. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

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

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

  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. 10-排序6 Sort with Swap(0, i) (25 分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  8. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

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

随机推荐

  1. 关于JVM的一些冷知识

    (1) Java加载类的一般顺序: 1.静态属性,静态方法声明,静态块. 2.动态属性,普通方法声明,构造块. 3.构造方法. 当加载一个类时,JVM会根据属性的数据类型第一时间赋默认值(一举生成的) ...

  2. 网络ping不通原因总结

    1.检查主机是否在线,硬件是否连接好 2.查看ip地址是否输入正确 3.查看是否防火墙拦截,如果有拦截关闭防火墙 4.两台电脑是否接入同一局域网(WiFi也是)

  3. uniq的坑坑

    很久没有做过文本统计之类的操作了,今天有点任务弄一下,幸亏机智的我列出了全部看了一遍,发现uniq的时候还是有重复的,然后总结了一下 假如我有1.txt这个文本: 10.0.0.1 10.0.0.1 ...

  4. [转]SQL server2008 导入超大SQL脚本文件(超过10M)

    同事给我一个sqlserver的学习库,sql脚本导出有300m,gui执行有内存溢出的错误报出来,所以问了一下度娘,学而时习之:) 1. SQL server2008 导入超大SQL脚本文件(超过1 ...

  5. flask-appbuilder +echarts 展示数据笔记

    pip install flask-appbuilder fabmanager create-app cd newapp fabmanager create-admin fabmanager run ...

  6. 为嵌入式mplayer移植添加ALSA音频驱动(全志V3s荔枝派zero)

    首先准备mplayer和alsa_lib,我的是bulidroot添加后编译自动下载的,版本分别是alsa-lib-1.1.4.1和mplayer-1.3.0. 首先编译alsa_lib: ./con ...

  7. 虚拟机网络连接NAT模式,本地用Xshell连接

    当虚拟机centos6网络连接使用NAT模式时,因为共用宿主机ip所以当使用Xshell时直接填写虚拟机的ip地址和22端口是无法连接虚拟机的. 这样就需要配置端口映射关系! 1. 打开虚拟网络编辑器 ...

  8. base64图片内容下载转为图片保存

    网页中的base64图片内容下载后,利用PIL转为图片保存 from skimage.io import imread from PIL import Image from cStringIO imp ...

  9. note 4 三大结构

    程序流程图 顺序结构 选择结构 if if-else if 语句-嵌套结构(Nested) 多分支结构(Chained) if score >= 90: print 'ARM' elif sco ...

  10. Redis为什么可以支持那么大的并发访问量?为什么redis没有单点并发瓶颈?

    一是redis使用内存 而是redis使用多路复用的IO模型: 现代的UNIX操作系统提供了select/poll/kqueue/epoll这样的系统调用,这些系统调用的功能是:你告知我一批套接字,当 ...