跪求时间复杂度QAQ
查看原帖
跪求时间复杂度QAQ
574802
Feuerwerk楼主2023/1/8 21:37

有没有大佬知道我这篇的时间复杂度啊,感觉时间复杂带度很大,但还是过了

#include<bits/stdc++.h>
using namespace std;

int n, m, k;
int a[10010], st[10010];
int ans = 0;
void dfs(int x){
	if(x > n){
		k ++;
		if(k == m + 1){
			for(int i = 1;i <= n;i ++) printf("%d ", a[i]);
			printf("\n%d\n", ans);
			exit(0);
		}
		return;
	}
	if(k == 0){
		for(int i = a[x]; i <= n;i ++){
			ans ++;
			if(st[i]) continue;
			st[i] = 1;
			a[x] = i;
			dfs(x + 1);
			st[i] = 0;
		}
	}
	else{
		for(int i = 1;i <= n;i ++){
			ans ++;
			if(st[i]) continue;
			st[i] = 1;
			a[x] = i;
			dfs(x + 1);
			st[i] = 0;
		}
	}
}

int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1;i <= n;i ++) scanf("%d", &a[i]);
	dfs(1);
	return 0;
}
2023/1/8 21:37
加载中...