A1067 Sort with Swap(0, i) (25 分)
一、技术总结
- 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数
- 输入时,用数组记录好每个数字所在的位置。
- 然后使用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 分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- 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 ...
- 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 ...
- PAT_A1067#Sort with Swap(0, i)
Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...
随机推荐
- [2019BUAA软工助教]助教学期总结
[2019BUAA软工助教]助教学期总结 一.量化自评 线上 博客点评:https://www.cnblogs.com/ChildishChange/MyComments.html 共 106 条 博 ...
- 【Java语言特性学习之三】Java4种对象引用
为了更灵活的控制对象的生命周期,在JDK1.2之后,引用被划分为(引用的级别和强度由高到低)强引用.软引用.弱引用.虚引用四种类型,每种类型有不同的生命周期,它们不同的地方就在于垃圾回收器对待它们会使 ...
- APP兼容性测试(三)测试方案设计
1. 背景 App的兼容性问题可能会导致流失潜在的客户,为了尽量减少因兼容性问题导致的同类问题,进一步提高产品的质量,提升用户体验,所以进行兼容性测试,尽可能多的保证更多的用户都可以正常使用app. ...
- Python 下载图片的三种方法
import os os.makedirs('./image/', exist_ok=True) IMAGE_URL = "http://image.nationalgeographic.c ...
- [新概念英语] Lesson 12 : GOODBYE AND GOOD LUCK
Lesson 12 : GOODBYE AND GOOD LUCK New words and expressions : luck (n) 运气 例句 You're not having much ...
- Spring Cloud Hystrix 熔断器(五)
序言 感觉hystrix很精彩,文档讲的也很好,这篇总结到哪里是哪里吧 写Hystrix之前,我们先简单的说说熔断器,和限流,这样你看完之后,就可以很容易理解Hystrix 熔断器 熔断器模式源于Ma ...
- 休谟:《人性论》一书中提出的要重视"是"与"应该"的区别
"价值"最初是经济学的范畴,指的是经济价值.商品价值.价值作的为一个哲学概念,首先大概是由18 世纪的英国哲学家休谟(David H ume,1711-1776)提出的.他于173 ...
- My time is limited
Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma - whic ...
- .net怎么使用Swagger
目录导航 一.安装 二.配置 三.调用 四.错误记录 一.安装 新建一个没有身份验证的mvc项目 - SwaggerMvc5Demo,然后添加一个名为Remote(自定义)且包含基础读写(不想手写)的 ...
- c#之添加window服务(定时任务)
本文讲述使用window服务创建定时任务 1.如图,新建项目,windows桌面->windows服务 2.如图,右键,添加安装程序 3.在下图安装程序 serviceInstaller1 上右 ...