1067 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. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:
Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}
Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤) followed by a permutation sequence of {0, 1, ..., N−1}. All the numbers in a line are separated by a space.
Output Specification:
For each case, simply print in a line the minimum number of swaps need to sort the given permutation.
Sample Input:
10
3 5 7 2 6 4 9 0 8 1
Sample Output:
9
题意:
给出一串数字,要求对这串数字进行排序,但是排序的过程中只能使用swap(0, i),即只能够用0来和另外一个数字交换。
思路:
用index[]数组来保存每个数字的下标,即index[0] = 3,表示数字0在数组中下标为3的位置处。如果下标和数字能够一一对应的话,两者就能够形成闭环的关系。例如{2, 0, 1}。
index[0] = 1;
index[1] = 2;
index[2] = 0;
我们可以通过一个while循环来让这个闭环中的部分数字回到自己正确的位置上去。
while (index[0] != 0) {
swap(index[0], index[index[0]]);
}
这样的闭环在一个数组中可能不止一个(如果排序完成的话,每一个数字都会单独的构成一个闭环),所以我们要遍历整个数组,确保每一个数字都应该在自己的位置上。如果0所在的闭环已经有序,但是index[i] != i; 这时候我们应该将0,插入到i所在的闭环中,在下一轮循环中将i所在中的闭环中的数字,尽可能的放在自己应该在的位置上。如此循环,直至满足题意。
Code:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 int n, t;
7 cin >> n;
8 vector<int> index(n+1);
9 for (int i = 0; i < n; ++i) {
10 cin >> t;
11 index[t] = i;
12 }
13 int count = 0;
14 for (int i = 1; i < n; ++i) {
15 if (i != index[i]) {
16 while (index[0] != 0) {
17 swap(index[0], index[index[0]]);
18 count++;
19 }
20 if (i != index[i]) {
21 swap(index[0], index[i]);
22 count++;
23 }
24 }
25 }
26 cout << count << endl;
27 return 0;
28 }
1067 Sort with Swap(0, i)的更多相关文章
- 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 ...
- 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 easy ...
- 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 ...
- 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 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 1067. Sort with Swap(0,*) (25)
时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...
- PTA 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 increasin ...
- PTA(Advanced Level)1067.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 ...
- 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 Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]
题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...
随机推荐
- Codeblocks支持语法着色
- 干货!!!测试如何确定是前端bug还是后端bug
目前的项目大多数都是前后端分离的,当我们发现bug后不知道指派给哪位开发,指派错了不仅影响解决bug 的效率,还容易被开发怼.最主要的是人家会认为你不专业,不专,不专呀.废话少说,上干货(踩过的坑)! ...
- 剑指 Offer 57. 和为s的两个数字 + 二分法 + 双指针
剑指 Offer 57. 和为s的两个数字 Offer_57 题目详情 使用二分法 package com.walegarrett.offer; /** * @Author WaleGarrett * ...
- 第十届蓝桥杯省赛-试题E: RSA 解密
试题E: RSA 解密 这里涉及到很多数论的知识:质因子分解,扩展欧几里得算法,快速幂算法,利用快速乘算法求解快速幂(mod太大导致不能直接乘,而是需要使用加法来替代乘法) 另外还需要注意扩展欧几里得 ...
- 面试准备——计算机网络(https)
一.为什么要提出HTTPS? HTTP的缺点: 明文通信.不加密,可能被窃听. 无身份验证,可能遭遇伪装. 无法证明报文的完整性,可能被篡改. 二.HTTPS = HTTP+加密(防窃听)+认证(防伪 ...
- Nginx重定向到其他端口
location / { # limit_req zone=test_req burst=5 nodelay; return 302 http://$host:3000/; } # 我这里的端口为30 ...
- Linux下基础命令
(1)ls(查看列表) (2)ls -l(查看列出文件详细信息) (3)ls -al (查看全部列出文件详细信息) (4)ls -dl(查看目录信息) (5)pwd(查看当前工作的目录) ...
- TensorFlow学习(2)
TensorFlow学习(2) 一.jupyter notebook的安装和使用 1. 什么是jupyter notebook jupyter notebook(http://jupyter.org/ ...
- H5 简单实现打砖块游戏
实现效果如图所示: 1.布局 在html中,声明 div1 作为作为带有边框的父物体,一切行为都要在 div1 中进行.创建小球ball.左右可滑动的板子bat,以及存放要销毁的砖块的父物体 bri ...
- sqlmap在https下的一种错误 - ssl连接失败
在昨天与师傅的交流中师傅考了我一个问题,在用sqlmap跑的时候遇到ssl爆红该怎么办,因为在实战中并没有遇到过这种情况,所以今天补一下知识. 首先查询了ssl的概念,通俗来说,如果一个网站没有安装s ...