求个大佬分析一下时间复杂度
查看原帖
求个大佬分析一下时间复杂度
1303781
wwy123楼主2024/9/26 20:06
//P8637 [蓝桥杯 2016 省 B] 交换瓶子
#include <iostream>
#include <algorithm>
int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int n;
	std::cin >> n;
	int vec[100000];
	for (int i = 1;i <= n;i++){
		std::cin >> vec[i];
	}
	long long ans = 0;
	for (int i = 1;i <= n;i++){
		while (vec[i] != i){
			std::swap(vec[i], vec[vec[i]]);
			ans++;
		}
	}
	std::cout << ans;
	return 0;
}
2024/9/26 20:06
加载中...