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

分两种情况:

如果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. VMWare桥接、NAT和only-host三种模式

    如果你想利用VMWare安装虚拟机,或想创建一个与网内其他机器相隔离的虚拟系统,进行特殊的调试工作.此时,对虚拟系统网络连接模式的选择就非常重要了.如果你选择的工作模式不正确,就无法实现上述目的,也就 ...

  2. 1213 How Many Tables 简单的并查集问题

    my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...

  3. PHP开发之路之一--WAMP的安装和配置

    来到新公司,领导说后面一个web系统不用ASP.NET做了,用国外的一个Drupal进行二次开发.这个Drupal是基于PHP的一款开源CMS系统,那就必须要自学PHP咯~ 接下来说说正题吧: 一.安 ...

  4. 全文检索工具推荐FileLocator

    全文检索工具推荐FileLocator https://www.baidu.com/link?url=_vaDZaJ_OePrAX-BTUD5hjTymnvN7_1oIAnWyS25hqxAg0nUH ...

  5. 1.1 selenium 安装

    1.在火狐浏览器的附加组件里搜索Selenium IDE

  6. springAOP 的pointcut

    <bean id="amqFilter" class="com.xxx.hotel.base.aspectj.AmQConsumerFilter"/> ...

  7. Lua 垃圾收集机制

    1. 问题:一款用Lua做的游戏,玩了一段时间后会变卡 因为知道lua是有自动管理内存的机制,所以之前一直没有关注过lua内存的问题.所以今天好好的查看了lua垃圾收集机制.看了一下Lua的Garba ...

  8. Hibernate 系列教程6-双向多对多

    双向多对多inverse配置 如果关系两边都使用默认inverse=false配置,表示关系两边都可以管理中间表的数据 关系其中一边设置inverse=true配置,表示自己放弃管理中间表关系,由对方 ...

  9. 火狐解决OCSP回应包含过期信息的问题

    连接 addons.mozilla.org 时发生错误. OCSP 回应包含过期信息. (错误码: sec_error_ocsp_old_response)hosts文件添加 vi /etc/host ...

  10. 配置App真机测试证书的流程 一览

    原文链接:http://www.jianshu.com/p/6b0de0d4c925 有开发者账号的前提下, 请进行如下步骤:1.首先登录网站:https://developer.apple.com. ...