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. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:
Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}
Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤105) followed by a permutation sequence of {0, 1, ..., N−1}. All the numbers in a line are separated by a space.
Output Specification:
For each case, simply print in a line the minimum number of swaps need to sort the given permutation.
Sample Input:
10
3 5 7 2 6 4 9 0 8 1
Sample Output:
注意点:1.a!=0别忘
2.k必须放在外面,否则超时!
#include<bits/stdc++.h>
using namespace std; const int maxn = 100010; int n; int pos[maxn]; int main(){ cin>>n; int a; int left=n-1,ans=0; for(int i=0;i<n;i++){
cin>>a;
pos[a]=i; if(pos[a]==a&&a!=0)//a!=0别忘
left--;
} int k=1; //k必须放在外面,否则超时 while(left>0){
if(pos[0]==0){ while(k<n){
if(pos[k]!=k){
swap(pos[0],pos[k]);
ans++;
break;
} k++;
} }else{
swap(pos[0],pos[pos[0]]);
left--;
ans++;
} } cout<<ans<<endl; }
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 ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- 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 ...
- 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\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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 easy ...
- 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 ...
随机推荐
- 阻抗匹配 及 SI9000 使用
1. 阻抗匹配 1. 波长 * 频率 = 光速(3*10^8) 2. PCB走线什么时候需要做阻抗匹配? 不主要看频率,而关键是看信号的边沿陡峭程度,即信号的上升/下降时间,一般认为如果信号的上升/下 ...
- camunda任务的一些简单操作
public class ZccTaskService { TaskService taskService; @Before public void init(){ ProcessEngineConf ...
- Cocos2d-x之Scene
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Scene场景也是cocos2dx中必不可少的元素,游戏中通常我们需要构建不同的场景(至少一个),游戏里关卡.版块的切换也就是一个一个场景 ...
- Android开发之程序猿必需要懂得Android的重要设计理念
前几天去參加了带着自己的作品去參加服务外包大赛,由于签位抽到的比較靠后就等待了蛮久,就跟坐在前面的一起參赛的选手開始讨论Android的开发经验.各自给对方展示了自己的作品,小伙伴就建议我看 ...
- Linux设置以root用户开机自动登录桌面
目录 Ubuntu 18.04系统下设置 Redhat7.6系统下设置 Ubuntu 18.04系统下设置 1. 允许使用root用户登录桌面 Ubuntu默认不允许使用root用户登录桌面的, ...
- JavaScript常用技巧之时间操作
1.获取当前时间戳 +new Date Date.parse(new Date())
- pom.xml解释
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- css3布局篇(双飞翼)
大家看到好多电商网站都见过经典三列布局,它也叫做圣杯布局 ,是Kevin Cornell在2006年提出的一个布局模型概念,这个在国内最早是由淘宝UED的工程师传播开来,在中国也有叫法是双飞翼布局,它 ...
- Shell5
sed文本编辑器(vim,notepad)非交互式的文本编辑器sed是逐行处理编辑器 sed [选项] ‘条件指令' 文件 #没有条件指令时,默认对全文所有进行修改sed的所有操作默认时在 ...
- Linux环境下安装PHP的gd库
当前使用的安装包版本: freetype-2.4.0.tar.bz2 jpegsrc.v9.tar.gz libpng-1.6.28.tar.gz 1.安装freetype tar jxvf free ...