1067. Sort with Swap(0,*) (25)
来源:互联网 发布:四层横移编程 编辑:程序博客网 时间:2024/06/05 18:54
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 (<=105) 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 1Sample Output:
9
#include<iostream>#include<vector>using namespace std;int main(){ int num[100005]; int n,t,c=0; cin>>n; for (int i=0;i<n;i++) { cin>>t; num[t]=i; } int sum=0; while(c<n) { if (num[0]==0) { for (c;c<n&&c==num[c];++c) continue; if (c>=n) break; swap(num[0],num[c]); sum++; } else { swap(num[0],num[num[0]]); sum++; } } cout<<sum; return 0;}
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- pat 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- [代码分享]用JS写中文版99乘法表
- 2017多校第二场 HDU 6052 To my boyfriend 思维,计数题
- 根据动态二维数组输出一个稀疏矩阵,根据我自己想输入的稀疏矩阵来输出对应的三元组
- MySql问题积累
- C++写时拷贝
- 1067. Sort with Swap(0,*) (25)
- 从Javascript向Chrome插件传递int参数的问题
- C语言:gets/puts输入输出字符串
- 【unity3d学习笔记】RPG 中控制相机距离拉近拉远以及旋转相机视角
- 软件设计原则-- 开放-封闭原则
- 浪里个浪 FZU
- HDU_【2017 Multi-University Training Contest 1】——1001 Add More Zero
- STL之迭代器与traits编程技法
- 11、Bootstrap--图片样式、辅助类样式及CSS组件