pat甲级1067 Sort with Swap(0, i)(贪心)

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 (≤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

本题题意:

           给定n个从 0 到 n - 1的数, 不按照顺序排列,

          此时能用0与这些数交换, 求出最小的交换次数 使得这些数能够按照升序排列

本题思路:

             贪心思想: 每次交换数字时 ,1.将 数值交换到对应的索引位置处就不再交换,

                                                              2.当0数值被交换到0位置, 此时使它与一个不满足适配条件的数(a[i] != i)再次交换

            因此将0设置为哨兵结点。

             1. 一开始数组下标存储的是 数字的位置, 数组值存的是 数值, 每次交换时, 需要通过for循环找到 n个数中与0下标相等的数 时间复杂度位 0(n) 会导致两个测试点超时

            看了柳神等大佬的思路后 数组下标存储 数字值, 数组值存储索引位置, 此时交换两个数时 只需要通过数组下标索引进行交换, 时间复杂度降到0(1)

超时的迭代代码:

#include<iostream>
using namespace std;
const int maxsize = 1e5 + 5;
int main(){
	int n, t, a[maxsize] = {0}, unAdapt = -1, steps = 0, k = 1;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &a[i]);
	}
	for(int i = 0; i < n; i++){
		if(a[i] == 0){
			t = i;
		}
		if(a[i] != i)
			unAdapt++; //记录需要交换的数字 这里的0不用算 eg  1 0  通 过 0 1 交换 只用交换1次 
	}
	//迭代实现
	while(unAdapt > 0){ //不适配数字决定迭代的次数
		if(t == 0){
			for(int i = k; i < n; i++){
				if(a[i] != i){
					swap(a[t], a[i]);
					t = i;
					steps++;
					k = i;
					break;
				}
			}
		}else{
			while(t != 0){
				swap(a[t], a[])
				for(int i = 0; i < n; i++){
					if(a[i] == t){
						swap(a[i], a[t]);
						t = i;
						steps++;
						unAdapt--;
						break;
					}
				}
			}
		}
	}
	printf("%d\n", steps); 
	return 0;
}

改进思路:

#include<iostream> 
using namespace std;
const int maxsize = 0x3f3f3f;
int n, a[maxsize];
int main(){
	int val, unAdapt = -1, steps = 0;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &val);
		a[val] = i;
	}
	for(int i = 0; i < n; i++){
//		if(a[i] != i &&  i != 0){ // adapt = n - 1 ; 
//			adapt--;
//		}
		if(a[i] != i)
			unAdapt++; //记录需要交换的数字 这里的0不用算 eg  1 0  通 过 0 1 交换 只用交换1次 
	}
	int k = 1; //定义k 为了优化时间复杂度, 交换元素时 从从第一个遍历到第n个 ,当第一个被遍历后 如果符合条件, 以后再次遍历它依然符合条件, 其余的元素同样如此 
	while(unAdapt > 0){
		while(a[0] != 0){
			swap(a[0], a[a[0]]); // 使哨兵位置所在元素的位置1 回到正确的位置, eg : 此时 a[0] = 1, 那么此时 a[1] 的正确位置该为 1 , 使a[0] ,与 a[a[0]]交换 
			steps++;
			unAdapt--;  // 交换一个位置后 不适配位置就少一个 
		}
		if(a[0] == 0){ //当哨兵位置0 回到原位时 为了使它继续交换使它和另外一个不适配元素进行交换 
			while(k < n){ //交换此步时 适配位置依然不变 
				if(a[k] != k){
					swap(a[0], a[k]);
					steps++;
					break;
				}
				k++;//表示此元素已经满足适配条件 a[i] = i 
			}
//				steps++; 不要写在外面 写在外面 k > n时也会加一次 
		}
	}
	printf("%d\n", steps);
	
	return 0;
}

参考柳神的简介代码:

#include<iostream>
using namespace std;
const int maxsize = 0x3f3f3f;
int n, a[maxsize];
int main(){
	int val, steps = 0;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &val);
		a[val] = i; //下标存储值, 值存储 位置 
	}
	for(int i = 1; i  < n; i++){ //从 0开始是因为当 0回到原位置时 需要从下标为一的第一个个元素开始判断是否有不适配元素 
		if(a[i] == i)
			continue;
		else{
			while(a[0] != 0){ //使哨兵位置回位 
				swap(a[0], a[a[0]]);
				steps++;
			}
			if(a[i] != i){ //交换后 a[i] 依然不等于 i 说明依然不适配 
				swap(a[0], a[i]); //此步将 已经 匹配好的哨兵位置 a[0] 与 不适配的 位置交换, 在随后的步骤, 哨兵(0)位置依然会回位 
				steps++;
			}
		}
	}
	printf("%d\n", steps);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值