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

分两种情况:

如果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. tableview cell添加3D动画

    当cell显示之前,会先调用该方法,因此给cell添加动画,在这个方法里面即可. -(void)tableView:(UITableView *)tableView willDisplayCell:( ...

  2. leetcode136 利用异或运算找不同的元素

    Given an array of integers, every element appears twice except for one. Find that single one. Note: ...

  3. SQL IO监控

    DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO { ON | OFF } SET STATISTICS TIME { ON | OFF }

  4. List-----Array

    1.Definition Arry数组是一种连续储存的List 储存方式:将线性表中的元素一次储存在连续的储存空间中. Computer's logical structure: 逻辑位置上相邻的元素 ...

  5. debian上安装lua编辑器

    Debian服务器上安装lua 1)下载压缩包 wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 2)解压文件 tar  zxvf lua-5.1.4.tar. ...

  6. WisKey的眼神

    WisKey的眼神 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  7. AFNetWorking 判断当前版本是否是最新版本

    NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@ ...

  8. IE下单选按钮隐藏后点击对应label无法选中的bug解决

    项目中,有时候填写表单我们的选项会隐藏掉radio或者checkbox,而只显示给用户对应的文字选择,如果用户点击label选择时,在FF/Chrome等标准浏览器中隐藏掉的radio/checkbo ...

  9. yum仅下载不安装

    通常是使用yum来安装解决依赖包关系,如果有一台服务器没法连接外网或yum源没有设置,希望通过另一台服务器将这些RPM包下载下来,然后再去安装.那么怎么使用yum工具来下载RPM包呢? 使用yum 要 ...

  10. WebRequest 对象的使用

    // 待请求的地址 string url = "http://www.cnblogs.com"; // 创建 WebRequest 对象,WebRequest 是抽象类,定义了请求 ...