一、技术总结

  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. V2Ray+WebSocket+TLS+Nginx 配置及使用

    v2ray 是一个模块化的代理工具,支持 VMess,Socks,HTTP,Shadowsocks 等等协议,并且附带很多高级功能,HTTP,TLS 等等. 关键词限制,全文 v2ray 中的 y 为 ...

  2. 关于@Autowired后Spring无法注入的问题

    1.对于新手来说,最明显的不过是在applicationContext.xml文件上没有加<context:component-scan base-package="com.xxx&q ...

  3. [笔记] vs code 设置终端

    设置文件: setting.json 1 设置自定义终端 cmd "terminal.integrated.shell.windows": "C:\\WINDOWS\\S ...

  4. SQL Server 跨服务器、跨版本使用复制 (2008、2012)

    在两台不同的服务器间实现SQL Server 的发布和订阅,需要一些设置. 测试环境:2008数据库.2012数据库,可实现跨版本发布订阅 本次测试是08的数据库做发布端 ,使用08数据及12数据库均 ...

  5. C# 去除数字中多于的0

    decimal i = decimal.Parse(Console.ReadLine()); Console.WriteLine((i).ToString(")); Console.Writ ...

  6. jenkins安装后提示localhost 拒绝了我们的连接请求。

    我是用msi文件安装的windows本地 ,安装文件看另外安装的博文. 此问题解决不是第一次安装方案 ,而是第一次安装完,使用也正常,关电脑再次访问的时候提示找不到 ,是因为本地服务没有启动  ,wi ...

  7. JDK1.8新特性——Stream API

    JDK1.8新特性——Stream API 摘要:本文主要学习了JDK1.8的新特性中有关Stream API的使用. 部分内容来自以下博客: https://blog.csdn.net/icarus ...

  8. Java自学-集合框架 LinkedList

    Java集合框架 LinkedList 序列分先进先出FIFO,先进后出FILO FIFO在Java中又叫Queue 队列 FILO在Java中又叫Stack 栈 示例 1 : LinkedList ...

  9. Mysql时区无法识别

    Unable to connect to database. Tried 1 times {:error_message=>“Java::JavaSql::SQLException: The s ...

  10. itextpdf5单元格中的段落没有行间距

    关于对表格中的段落没有行间距的解决方式:通过观察和推测的结论:itextpdf对一些属性,只会对最外层元素的属性进行接收处理,如行间距.例: Paragraph p = new Paragraph(1 ...