PAT 1067 Sort with Swap[难]
1067 Sort with Swap(0,*) (25)(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 (<=10^5^) 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:
9
题目大意:现在只有swap(0,*)这个操作来实现排序,也就是将0和*进行交换排序,找出最少的操作次数。
//我不太会,我只能想到是的有点像快排,当0所在的下标<n/2时,就从右边开始遍历选最小的数换,反之取最大的数换,但是这样明显不是次数最少的,所以我不会。
代码来自:https://www.liuchuo.net/archives/2301
#include <cstdio>
#include <vector>
using namespace std;
int main() {
int n, num, cnt = , ans = , index = ;
scanf("%d", &n);
vector<int> v(n);
for(int i = ; i < n; i++) {
scanf("%d", &num);
v[num] = i;//保存当前数所在的位置。
if(num != i && num != ) cnt++;
}
while(cnt > ) {
if(v[] == ) {
while(index < n) {
if(v[index] != index) {
swap(v[index], v[]);//直接用swap函数是多么简单。
ans++;
break;
}
index++;
}
}
while(v[] != ) {
swap(v[v[]], v[]);
ans++;
cnt--;
}
}
printf("%d", ans);
return ;
}
//当时看解题思路,似乎懂了一些,但是自己写还是不对,真是绝望,好笨。
1.使用向量记录的是数字所在的位置,而不是当前位置放的是谁,这样就简单了,因为每次都要找数字所在的下标位置。
2.使用index来记录,因为每次循环到index之前的都已经存储好了,index是指数1,数2,数3是否存到了正确的位置。
3.当0不在0的位置上时,一直循环交换即可。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int a[];
int main() {
int n,index=;//index指向0所在的位置。
cin>>n;
int ct=;//标记有多少不在原位的,在原位的肯定就不去挪了。
for(int i=;i<n;i++){
cin>>a[i];
if(a[i]==)
index=i;//标记住i的下标。
if(a[i]!=i&&i!=
)
ct++;
}
int t,no=;
while(ct!=){
if(index!=){
for(int i=;i<n;i++)
if(a[i]==index){
t=i;break;
}
a[t]=;
a[index]=index;
index=t;
ct--;
no++;
}else if(index==){
for(int i=;i<n;i++)
if(a[i]!=i){
t=i;break;
}
if(t==-)break;
a[]=t; a[t]=;
no++;
}
cout<<"hh";
t=-;
}
cout<<no; return ;
}
//终于理解了为什么自己错了,因为当0回到0位置时,应该选取1,2,3依此数字最小的而不是位置上不等于当前位置的!
学习了!
PAT 1067 Sort with Swap[难]的更多相关文章
- 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 ...
- 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 ...
- 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 分)
1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...
- PTA 1067 Sort with Swap(0, i) (贪心)
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...
- 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 ...
- PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 5 ...
随机推荐
- c++ 友元类 与 友元类派生类
定义: 当一个类B成为了另外一个类A的“朋友”时,那么类A的私有和保护的数据成员就可以被类B访问.我们就把类B叫做类A的友元. 用法: 在A类中加入: friend class B; 下面这个程序说明 ...
- Qt编写视频播放器(vlc内核)
在研究qt+vlc的过程中,就想直接做个播放器用于独立的项目,vlc还支持硬件加速,不过部分电脑硬件不支持除外.用vlc的内核写播放器就是快,直接调用api就行,逻辑处理和ui展示基本上分分钟的事情, ...
- C#设计模式--设配器模式
0.C#设计模式-简单工厂模式 1.C#设计模式--工厂方法模式 2.C#设计模式--抽象工厂模式 3.C#设计模式--单例模式 4.C#设计模式--建造者模式 5.C#设计模式--原型模式 设计模式 ...
- Android 框架
1. https://github.com/wyouflf/xUtils xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构, ...
- Linux命令 uname:查看系统与内核相关信息
zh@zh:~$uname --help zh@zh:~$uname -a //所有系统相关的信息
- Elasticsearch学习之深入搜索五 --- phrase matching搜索技术
1. 近似匹配 什么是近似匹配,两个句子 java is my favourite programming language, and I also think spark is a very goo ...
- 2-5 vue基础语法
一.vue基础语法 语法: {{msg}} html赋值: v-html="" 绑定属性: v-bind:id="" 使用表达式: {{ok? "ye ...
- jquery.fn.extend与jquery.extend用法与区别
jQuery为开发插件提拱了两个方法,分别是: 代码如下 复制代码 jQuery.fn.extend(object); 和 jQuery.extend(object); jQuery.exte ...
- 微信小程序---示例DEMO
转:CSDN的文章: https://blog.csdn.net/rolan1993/article/details/73467867 不错的DEMO: https://github.com/skyv ...
- linux下配置Java_web环境
I安装JDK 1下载jdk*.bin/jdk*.rpm文件 2把安装包copy到安装目录 cp jdk*.bin /www/java 3安装JDK 如果是.bin文件,直接在复制的当前目录执行即可 . ...