一、技术总结

  1. 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数
  2. 输入时,用数组记录好每个数字所在的位置。
  3. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自己的位置上,如果没有一直与其他的数字进行交换。如果这个过程中,i回到自己位置上就不用调换,如果不是那么直接与0位置进行调换,直到所有位置都到指定位置上面。

二、参考代码

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n, ans = 0, a[100010], t;
cin >> n;
for(int i = 0; i < n; i++){
cin >> t;
a[t] = i;
}
for(int i = 1; i < n; i++){
if(i != a[i]){
while(a[0] != 0){
swap(a[0], a[a[0]]);
ans++;
}
if(i != a[i]){
swap(a[0], a[i]);
ans++;
}
}
}
cout << ans;
return 0; }

A1067 Sort with Swap(0, i) (25 分)的更多相关文章

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

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

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

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

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

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

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

  7. A1067. Sort with Swap(0,*)

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

  8. PAT甲级——A1067 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 ...

  9. 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. gradle 多模块Springboot项目 compile project引用其他模块的坑

    本来以为子项目中compile project(':xxx'),就能引用其他模块了,因为之后idea也没在引用时候标红 然而我gradle build的时候,居然各种找不到引用模块的类 最后在stac ...

  2. VMware exsi虚拟机磁盘扩容

    创建Linux时分配磁盘空间随着使用的增加,使用率逐渐升高,需要对/root进行扩容,此时需要在添加或者扩展一下磁盘. 查看Linux版本信息 [root@localhost ~]# cat /etc ...

  3. 【Oracle】RMAN duplicate复制库

    基础环境: 172.17.4.60 操作系统:Linux 6.4 数据库:Oracle11gR2 (源数据库) 172.17.4.61 操作系统:Linux 6.4 数据库:Oracle11gR2 ( ...

  4. tsconfig.json配置项详解

    { "compilerOptions": { "allowUnreachableCode": true, // 不报告执行不到的代码错误. "allo ...

  5. sap和OA之间数值传递2(工程创建)

    1.创建project. 右击--new-other

  6. Enum.GetUnderlyingType(obj.GetType())

    Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:

  7. 百度编辑器UEditor,保存图片的配置问题

    前言: 在使用百度编辑器UEditor的时候,如何将图片保存到服务器,我刚开始以为是要自己写上传文件的方法,后来发现只需要配置一下即可,如果你也正在使用百度富文本编辑器UEditor的话,这篇文章将非 ...

  8. .NET Core跨平台部署于Docker(Centos)- 视频教程

    (双击全屏播放) 往期教程: .NET开发框架(一)-框架介绍与视频演示 .NET开发框架(二)-框架功能简述 .NET开发框架(三)-高可用服务器端设计 .NET开发框架(四)-服务器IIS实践教程 ...

  9. c# .NET Framework 版本确定

    关于.NET Framework 版本信息这里做个介绍: 1. 编译时,工程的目标的 .NET Framework 版本 同样的代码,我先选择.net 4.0,就发现有语法错误,原因是4.0版本还没提 ...

  10. word转html预览

    #region Index页面 /// <summary> /// Index页面 /// </summary> /// <paramname="url&quo ...