【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\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
随机推荐
- Tomcat配置过程
Tomcat的配置其实还是挺简单的,下面是在Myeclipse中配置 1.首先要在Tomcat官网下载,网址:http://tomcat.apache.org/,然后左侧会有Download,选择你要 ...
- python如何将自己写的代码打包供他人使用
背景: 利用setuptools来实现,所以先安装setuptools,其实Python2.7.9及之后的版本都自带安装setuptools了,无需在另外安装 如果没有安装setuptools的直接下 ...
- IntelliJ IDEA 2017.3尚硅谷-----创建工程
- 普及C组第二题(8.4)
2266. 古代人的难题 (File IO): input:puzzle.in output:puzzle.out 时间限制: 1000 ms 空间限制: 60000 KB 题目: 门打开了, 里面 ...
- 第一struts2程序
今天学习了第一个struts2程序 第一步建立一个java web程序 第二步 加入jar包 第三步 添加两个jsp文件 login.jsp和result.jsp文件 login.jsp 在body部 ...
- Reinforcement Learning Algorithm 资源
算法源码: https://github.com/ljpzzz/machinelearning https://github.com/imraviagrawal/Reinforcement-Learn ...
- redis集群(Sentinel)
问题 Redis 主哨兵模式是如何保证高可用的 主要依赖主哨兵的发现故障和故障转移 概述 本文假设读者对redis 的主从复制已经进行了了解 . Redis 主哨兵集群为Redis 提供了高可用,即高 ...
- 前端——语言——Core JS——《The good part》读书笔记——附录三,四,五(JSLint,铁路图,JSON)
1.JSLint 本书的JSLint部分只是一个引言,详细了解该工具的使用参考http://www.jslint.com/ 2.铁路图 在本书中使用过的铁路图集中放在这部分附录中,其实读完本书之后,没 ...
- Spring - 周边设施 - H2 数据库启动时写入数据
1. 概述 之前讲到了 H2 的引入 这下我想说说 H2 启动时的 数据导入 2. 场景 需求 启动项目后, H2 启动起来 环境数据会自动注入 H2 数据库 可以验证是否成功 3. 环境 os wi ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...