对于一个由0到N-1的序列,如果只能交换0和另一个数的位置,求多少次能够将序列变为递增序列。

输入为<N> <序列>(N和序列之间有一个空格,序列元素之间均有一个空格)。

设序列存储在数组A里。

一个直接的思路是将0和0所在位置应有的元素交换,如果0到达了0号位置但是序列没有完全变成递增序列,则让0和一个没有归位的元素进行一个废交换,然后再进行后续的交换。

一个独特的思路是,将0应在位置的元素(即A[0])换到它应该在的位置(例如A[0]=3,则把A[0]和A[3]交换),不断这样交换,直到0真正到了0位置,判断一下是否全部归位,不然的话就把0换到第一个没有归位的元素上,其实这一步就是上面的废交换,接下来再重复进行上面的交换,直到序列变为递增序列。要注意的是,每次检索第一个没有归位的元素时,起步的值根据A[0]所交换的位置开始,否则会超时。下面的算法用这个思路实现。

#include <iostream>
using namespace std; int FirstNotRight(int* A, int begin, int end){
for (int i = begin; i <= end; i++){
if (*(A + i) != i)
return i;
}
return 0;
} void Swap(int* a, int *b){
int temp = *a;
*a = *b;
*b = temp;
} int main(){
int N;
int input;
cin >> N;
int* A = (int*)malloc((N + 1)*sizeof(int));
for (int i = 0; i < N; i++){
scanf("%d", &input);
*(A + i) = input;
} int cur = FirstNotRight(A, 1, N - 1);
int count = 0;
while (cur != 0){ if (*A == 0){
Swap(A, A + cur);
count++;
} while (*A != 0){
int curtoswap = *A;
Swap(A, A + curtoswap);
count++;
} cur = FirstNotRight(A, cur, N - 1); }
cout << count << endl;
return 0;
}

排序算法(二)Sort with Swap(0,*)的更多相关文章

  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. 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现

    选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...

  3. Pat1067:Sort with Swap(0,*)

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

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

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

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

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

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

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

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

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

  10. PAT_A1067#Sort with Swap(0, i)

    Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...

随机推荐

  1. Android的Ui层次

    UI 概览 Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成.View 对象用于在屏幕上绘制可供用户交互的内容.ViewGroup 对象用于储存其他 V ...

  2. Apache shiro集群实现 (五)分布式集群系统下的高可用session解决方案

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  3. CRM 2013 Script Loading Deep Dive

    关于CRM中脚本的加载次序梳理的很不错,可以看看 https://community.dynamics.com/crm/b/develop1/archive/2013/11/02/crm-2013-s ...

  4. ERROR: In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing

    eclipse  sdk从低版本切换到高版本sdk的时候   v7包会包这个错ERROR: In <declare-styleable> MenuView, unable to find ...

  5. 使用MD5SUM检查文件

    有不少网站提供下载文件的同时,提供了文件的MD5SUM的值.如何检查自己下载的文件与原文件一样呢?用md5sum的-c选项. 操作如下: 1.先新建一个文本文件,写入网站上提供的md5sum的值,空两 ...

  6. Android Multimedia框架总结(十四)Camera框架初识及自定义相机案例

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52738492 前言:国庆节告一段 ...

  7. Spring的DataSource配置、将Hibernate配置全部写到Spring配置

    DataSource可以集中管理数据库连接,减少维护工作量,使部署更简单: Spring的DataSource配置:(Spring数据源配置)这里使用dbcp,还有很多其他的如c3p0,jdbc,jn ...

  8. 求链表倒数第n个元素

    提示:设置一前一后两个指针,一个指针步长为1,另一个指针步长为n,当一个指针走到链表尾端时, 另一指针指向的元素即为链表倒数第n个元素. #include <stdio.h> #inclu ...

  9. velocity map list 数组操作

    Velocity生成模板的时候,经常需要使用到map.list对象,然后遍历输出对象的属性值.当你需要遍历的时候记录遍历的步长的时候,可以使用$velocityCount内置变量进行输出.下面demo ...

  10. Git 解决一个电脑多用户情况(win7)

    首先:在输入ssh-keygen -t rsa -C "注册邮箱"后不要急着按enter,此时输入秘钥对的文件名,不要跟默认文件重名(默认的是id_rsa) 划红线的地方就是新的文 ...