-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15666.cpp
More file actions
52 lines (47 loc) · 1.06 KB
/
15666.cpp
File metadata and controls
52 lines (47 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <algorithm>
#define io ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
int N,M;
int nums[9];
int res[9];
bool visited[9];
bool promising(int n,int depth){
if(res[depth-1]<=n)return true;
return false;
}
void backtracking(int n,int depth){
if(promising(n,depth)){
if(depth==M){
for(int i=1;i<=M;i++){
cout<<res[i]<<' ';
}
cout<<'\n';
}else{
fill(visited,visited+9,false);
for(int i=1;i<=N;i++){
if(visited[i]==false){
for(int j=1;j<=N;j++){
if(j!=i&&nums[i]==nums[j]){
visited[j]=true;
}
}
res[depth+1]=nums[i];
backtracking(nums[i],depth+1);
}
}
}
}
}
int main()
{
io;
cin>>N>>M;
for(int i=1;i<=N;i++){
cin>>nums[i];
}
sort(nums+1,nums+N+1);
backtracking(0,0);
return 0;
}