贪心算法

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

1:3 5 0 2 6 4 9 7 8 1
2:3 5 2 0 6 4 9 7 8 1
3:0 5 2 3 6 4 9 7 8 1 (如果0处在自己位置上,那么找一个不在自己位置上的数val与之交换)
4:5 0 2 3 6 4 9 7 8 1
5:5 1 2 3 6 4 9 7 8 0
6:5 1 2 3 6 4 0 7 8 9
7:5 1 2 3 0 4 6 7 8 9
8:5 1 2 3 4 0 6 7 8 9
9:0 1 2 3 4 5 6 7 8 9

在例子第3步交换中,如果每次都线性查找,时间复杂度就会很高,会有2组样例超时。
这里用set优化了下速度。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
const int maxn=+;
int a[maxn];
int pos[maxn];
int cnt;
set<int> leftnum; //存储了还没有被放到正确位置上的数字(0除外)
int main()
{
int n;
scanf("%d",&n);
cnt=n; //还未排好序的个数
for(int i=;i<n;i++){
scanf("%d",&a[i]);
pos[a[i]]=i;
if(i==a[i])
cnt--;
else if(a[i]!=)
leftnum.insert(a[i]);
}
int p0=pos[]; //0目前所在的位置
int p;
int ans=;
while(cnt){
//将0和应处在0位置的数(p0)交换位置
a[p0]=p0;
leftnum.erase(p0);
p0=pos[p0];
a[p0]=;
ans++;
cnt--;
if(p0==)
cnt--;
//如果0在自己位置上,但还没有排好序
if(p0== && cnt){
//这里用set来优化寻找还没有处在自己位置上的数字,将0与之交换
int val=*leftnum.begin();
int tmp=pos[val];
pos[val]=p0;
a[p0]=val;
p0=tmp;
a[p0]=;
ans++;
cnt++;
}
}
printf("%d",ans);
return ;
}

PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法的更多相关文章

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

  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. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

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

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

  5. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)

    只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...

  6. PAT甲题题解-1039. Course List for Student (25)-建立映射+vector

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789157.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题

    题目简单,但解题的思路需要转换一下,按常规思路肯定超时,推荐~ 题意:给出n个人的姓名.年龄和拥有的钱,然后进行k次查询,输出年龄在[amin,amx]内的前m个最富有的人的信息.如果财富值相同就就先 ...

  8. PAT甲题题解-1002. A+B for Polynomials (25)-多项式相加

    注意两点:1.系数也有可能加起来为负!!!一开始我if里面判断为>0导致有样例没过...2.如果最后所有指数的系数都为0,输出一个0即可,原本以为是输出 1 0 0.0... #include ...

  9. PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)

    就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...

随机推荐

  1. T-SQL应用实例

    实验一:实验案例一(附加“练习用的可以附加的数据库--class”) 1.在products表中查询出厂日期晚于2014年4月的水果信息. select * from products  where  ...

  2. 模拟的confirm

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  3. leetcode 6. ZigZag Conversion [java]

    自己写的: if(numRows == 1) return s; int ll = s.length() / 2 + 1; Character tc[] = new Character[numRows ...

  4. Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

      在开发中用到Microsoft.ACE.OLEDB.12.0,但是,出现了Microsoft.ACE.OLEDB.12.0' provider is not registered on the l ...

  5. kdTree相关原理及c++实现

    kdTree概念 kd-tree或者k维树是计算机科学中使用的一种数据结构,用来组织表示k维空间中点的集合.它是一种带有其他约束条件的二分查找树.Kd-tree对于区间和近邻搜索十分有用.一般位于三维 ...

  6. python爬虫(三)

    webdriver Selenium是ThroughtWorks公司开发的一套Web自动化测试工具.它分为三个组件:Selenium IDE,Selenium RC (Remote Control), ...

  7. [BeiJing2006]狼抓兔子

    题面 一眼看就是最小割板子题,建图也很直观,注意每一条边建双向边其实不用建4条边,只要反向边的容量和正边相同就行.然后直接跑最大流板子就行.不过此题拿vector存图会MLE……而拿链前存图就能卡过去 ...

  8. pytorch 绘制训练曲线;服务器端训练,本地浏览器显示,本地打不开;tensorboard端口被占

    代码里面用tensorboard保存了训练的日志在logs目录里面 用tensorboard命令打开日志目录:tensorboard --logdir="./logs/" 会显示一 ...

  9. 安装 Autoconf, Automake & Libtool

    今天在使用sudo apt-get install命令安装autoconf和automake时,出现了问题,说是不能sudo apt-get install安装这些软件似乎不是最新的.由此,我通过搜索 ...

  10. MVC在母版页设置子页面的css和js引用布局

    1.在母版页中指定子页面css和js布局 @RenderSection("Styles", false) @RenderSection("Scripts", f ...