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, ...
随机推荐
- LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum
公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子 ...
- MySQL锁表查询SQL
// 查看进程 SHOW PROCESSLIST; // 查看是否锁表 SHOW OPEN TABLES WHERE In_use > 0; // 查看正在锁的事务 SELECT * FROM ...
- Knative 初体验:CICD 极速入门
Knative 社区很早就在讨论用 Tekton 替换 Build 模块的相关事宜.Knative Build 官方已经正式说明不再建议使用 Knative Build 了. 如果你知道 Knativ ...
- C#中一行代码实现18位数字时间戳转换为DateTime
场景 存取的时间戳数据为: 636728674347302002 怎样将其转换为DateTime时间. 目前大多数的策略是,转换成string,然后 DateTime dateTimeStart = ...
- addEventListener和JavaScript的事件机制
JavaScript的事件处理分为两个阶段: 捕获阶段:从根节点向event.target层层传递 冒泡阶段:从event.target向根节点层层传递 addEventListener(eventN ...
- eclipse 导出 jar包详细步骤
如图所示:
- python3装饰器
由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...
- To B产品,业务方全程蒙蔽怎么搞?
这是发生在很久前的事,那会我还是产品实习生. 今天和业务部门进行需求审核,对的是公司内部SAAS系统的采购模块.怎么说呢?就是觉得不专业吧 辛辛苦苦把原 ...
- itextpdf确定页面坐标的方式
itextpdf的确定页面上的具体坐标是通过坐标来确定的. 它们的坐标轴是这样的:以左下角为原点的xy坐标轴. 在itextpdf的方法中中,定义某个点的位置,都是以左下方为原点坐标轴来确定. 如果要 ...
- spring的组件使用
源代码下载:https://www.lanzous.com/i5p4mvc * 组件扫描 * @Component:表示这个类需要在应用程序中被创建 * @ComponentScan:自动发现应用程序 ...