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\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
随机推荐
- 数据结构笔记02:Java面试必问算法题
1. 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min()的 ...
- 为Mono安装MySql连接驱动
为Mono安装MySql连接驱动(转) 2013 年 1 月 24 日.NETmono.MySql DOTNET and Mono by default only support database c ...
- 在vs环境中跑动sift特征提取(原理部分)
/* 如果给两张图片,中间有相似点.要求做匹配.怎么做.我现在能讲么? 比如给了两幅图片,先求出sift点. 尺度空间极值检测.高斯模糊 关键点定位 关键点方向确定 关键点描述 kdtre ...
- LeetCode 226
Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 /** ...
- [改善Java代码]易变业务使用脚本语言编写
建议16: 易变业务使用脚本语言编写 Java世界一直在遭受着异种语言的入侵,比如PHP.Ruby.Groovy.JavaScript等,这些“入侵者”都有一个共同特征:全是同一类语言—脚本语言,它们 ...
- mac端口占用查找进程并杀死
查找端口port被占用的进程 lsof -i tcp:port 杀死进程pid kill -9 pid
- poj3393[模拟题]
Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- MVC5----用户登陆及验证码
随便写写记录一下学习的过程 登陆 Models中添加添加 public class LoginViewModel { [Required(ErrorMessage = "*")] ...
- 设置In_Memery
alter system set inmemory_size=4G scope=spfile; alter table table_name inmemory; alter table table_n ...
- ### OpenCV安装(Linux)
### OpenCV安装(Linux) @(gr_self)[ffmpeg | openCV] #@author: gr #@date: 2015-09-02 #@email: forgerui@gm ...