【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意:
输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列)。每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
int ans=n-;
for(int i=;i<n;++i){
cin>>a[i];
b[a[i]]=i;
if(a[i]==i&&a[i])
--ans;
}
int sum=;
int pos=;
while(ans)
if(b[]!=){
swap(b[],b[b[]]);
++sum;
--ans;
}
else
for(int i=pos;i<n;++i)
if(b[i]!=i){
swap(b[],b[i]);
++sum;
pos=i+;
break;
}
cout<<sum;
return ;
}
【PAT甲级】1067 Sort with Swap(0, i) (25 分)的更多相关文章
- 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 ...
- 1067 Sort with Swap(0, i) (25 分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- 1067 Sort with Swap(0, i) (25分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- PTA 10-排序6 Sort with Swap(0, i) (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i) (25分) Given a ...
- PAT甲级——A1067 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 ...
- 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 ...
- 10-排序6 Sort with Swap(0, i) (25 分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- A1067 Sort with Swap(0, i) (25 分)
一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
随机推荐
- Python记:索引操作示例:将以数指定年,月,日的日期打印出来
————————————————————————————————————不要停止奔跑,不要回顾来路,来路无可眷恋,值得期待的只有前方. months=[ 'January', 'February', ...
- [控制台尊享] MinGW下使用 gotoxy函数
相信用过Turbo C的童鞋都知道gotoxy这个函数吧,但由于某些原因,windows下的mingw没有直接提供这个函数. 那么gotoxy究竟是干什么的呢?假设你的程序是基于控制台的(就是一个黑窗 ...
- Python - 标准库部分函数、类的大致实现(持续更新)
all() def all(iterable): for element in iterbale: if not element: return False return True any() def ...
- 1.java-谈谈接口
1.面向接口给程序带来的便利和灵活性 List li = new ArrayLIst(); 为什么不写成 ArrayList li = new ArrayLIst(); 2.接口就相当于一些类的规范, ...
- go使用错误概览
1. 解决:GO语言中要提供给外面访问的方法或是结构体必须是首字母大写.这个结构体只有结构体名大写了,而里面的字段没有首字母大写,而GO语言在模板调用时应该认为是两个不同的过程,所以找不到值.于是把结 ...
- 树链剖分 (ZQU1607)
这道题与模板之间,多了个确定哪个为根的操作: 这道题有技巧,并不需要真正去建立以某个节点为根的树 关于路径的操作,无论以哪个点为根,得出的答案无影响: 关于对子节点进行操作的,有几种情况, 当查询节点 ...
- Atcoder Beginner Contest151D(迷宫问题求任意两点最短路径的最大值,BFS)
BFS可以求得最短路,DFS会找到从当前点到图中叶子结点的路径. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using na ...
- GO学习之 安装Go语言及搭建Go语言开发环境
一.下载 1.下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ 2.版本的选择 Windows ...
- python pip 安装常用库如何使用国内镜像源
最近通过python安装一些常用库发现下载速度特别慢,而且还会出现无法安装等错误,尝试找了一些国内的镜像源,发现阿里云的速度很快,这里做个记录,并分享给大家. 国内镜像源 阿里云:http://mir ...
- acm数论之旅--中国剩余定理
ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯) 中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 ...