只对没有归位的数进行交换。

分两种情况:

如果0在最前面,那么随便拿一个没有归位的数和0交换位置。

如果0不在最前面,那么必然可以归位一个数字,将那个数字归位。

这样模拟一下即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; int n;
int a[+];
queue<int>Q;
int pos[+]; int main()
{
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) pos[a[i]]=i;
for(int i=;i<n;i++)
{
if(a[i]==) continue;
if(a[i]!=i) Q.push(a[i]);
} int ans=;
while()
{
if(a[]==)
{
while(!Q.empty())
{
int head=Q.front(); Q.pop();
if(a[head]==head) continue;
else
{
ans++;
int pos1=pos[];
int pos2=pos[head];
swap(a[pos1],a[pos2]);
swap(pos[head],pos[]);
break;
}
}
if(Q.empty()) break;
}
else
{
ans++;
int pos1=pos[];
int pos2=pos[pos[]];
int num1=;
int num2=pos[];
swap(a[pos1],a[pos2]);
swap(pos[num1],pos[num2]);
}
}
printf("%d\n",ans);
return ;
}

PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 1067. Sort with Swap(0,*) (25)

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...

  5. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

  6. PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法

    贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 ...

  7. 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 ...

  8. pat1067. Sort with Swap(0,*) (25)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  9. 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 ...

随机推荐

  1. Windows常用的监视数据指标

  2. 推荐两个界面原型设计工具--GUIDesignStudio 和 Mockups For Desktop

    前段时间,有幸参加一次高级软件架构师的培训,授课老师介绍了两个很好玩的界面原型设计工具:GUIDesignStudio 和 Mockups For Desktop,现分享一下,截图说明,洗洗眼球,权当 ...

  3. Hadoop中的一些基本操作

    先粗略说一下“hadoop fs”和“hadoop dfs”的区别:fs是各比较抽象的层面,在分布式环境中,fs就是dfs,但在本地环境中,fs是local file system,这个时候dfs不可 ...

  4. linux下base命令

    查看base那么命令的帮助: root@OpenWrt:/# basename --help BusyBox v1.22.1 (2015-09-15 16:38:30 CST) multi-call ...

  5. Redis如何保存数组和对象

    个人建议使用PHP自带的序列化函数serialize和unserialize函数 我们可以封装一个自己的Redis类 <?php class MyRedis{ private static $h ...

  6. Linux SSH使用公钥私钥实现免登陆

    公钥和私钥(我是文盲,钥字之前都是读yao,这是多音字这里应该念yue),是成对出现的,一旦任何一个做了更改都会验证失败. 1.免登陆的实现:   使用下例中ssky-keygen和ssh-copy- ...

  7. Inno Setup入门(八)——有选择性的安装文件

    这主要使用[Components]段实现,一个演示的代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName=" ...

  8. JS-日期框、下拉框、全选复选框

    <!-- 下拉框 --><link rel="stylesheet" href="static/ace/css/chosen.css" /&g ...

  9. CSS Day04 css核心基础

    1.在HTML中引入CSS的方法 (1)行内式 行内式即在标记的style属性中设定CSS样式,这种方式本质上没有体现出CSS的优势,因此不推荐使用.  例如:  <h1 style=“colo ...

  10. HDU5908 Abelian Period 暴力

    题目大意:将一个数组分成长度为k的几个连续区间,如果每个区间内各个元素出现的次数相同,则称k为一个阿贝尔周期,从小到大打印所有阿贝尔周期,数据间加空格. 题目思路:map+暴力 #include< ...