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 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 1
Sample Output:
9
#include <stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n;
int loc[];
while(scanf("%d",&n)!=EOF)
{ int tem,i,left;
left = n-;
for(i=;i<n;i++)
{
scanf("%d",&tem);
loc[tem] = i;
if(tem == i && i!=)
--left;
} int count = ;
int k = ;
while(left > )
{
if(loc[]==)
{
while(k < n)
{
if( loc[k] != k )
{
swap(loc[],loc[k]);
++count;
break;
}
++k;
}
} while(loc[]!=)
{
swap(loc[],loc[loc[]]);
++count;
--left;
} } printf("%d\n",count);
}
return ;
}
1067. Sort with Swap(0,*) (25)的更多相关文章
- 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 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 ...
- PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 5 ...
- 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 ...
- pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
- 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\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
随机推荐
- Python 2.x and 3.x String VS Bytes
In Python 3 unicode strings are the 'regular strings' (str) and byte strings are separate objects. L ...
- [辛酸历程]在Mac中使用Python获取屏幕截图
一.起因 最近想做个小外挂玩玩,技术倒是不难,就是通过图片匹配加上一些判断方法来刷分.但是在最不起眼(却最容易出问题)的准备阶段卡住了. 为什么卡住了呢,简单说,因为我需要获取截屏的数据,所以就要找一 ...
- python(6)-shutil模块
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中: #源码 def copyfileobj(fsr ...
- jboss之mod_cluster集群
本篇针对的mode_cluster版本是mod_cluster-1.2.6.Final-linux2-x64.tar.gz / mod_cluster-1.2.6.Final-windows-amd6 ...
- java按行读取txt并按行写入
IO流想必大家都很熟悉了,本次实现的需求是按行读取文件内容并且按行写入,代码如下: try { String encoding="utf-8"; //设定自己需要的字符编码集 Fi ...
- Android之Android studio安装
首先下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后下载Android SDK:http://ww ...
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();
只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called durin ...
- js 比较两个日期的大小的例子
例子,直接比较大小即可 代码如下 复制代码 <script>var st="2009-10-20 14:38:40"var et="2009-10-20 15 ...
- Oracle工程师技能树
整理了份Oracle工程师的技能树,方便大家在学习的过程中有个大体方向. 欢迎提意见,可以随时更新. 源文件链接地址点此 图片如下: 多媒体插件如下:
- Repeater内RadioButton.GroupName失效
最近在做项目时遇到要在repeater中显示多个radiobutton并且实现单选功能,于是很自然地就加上了GroupName,但事实是不行的,在repeater中的radiobutton呈现到页面的 ...