-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOJ_03.cpp
More file actions
52 lines (49 loc) · 1.36 KB
/
Copy pathOJ_03.cpp
File metadata and controls
52 lines (49 loc) · 1.36 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 <stdio.h>
#include <algorithm>
using namespace std;
bool isCatalan(int* tar, int len){
int cnt_zero = 0, cnt_one = 0;
for(int i = 0; i < len; i ++){
if(tar[i] == 0) cnt_zero++;
if(tar[i] == 1) cnt_one++;
if(cnt_zero < cnt_one) return false;
}
return true;
}
int main(){
int n, cnt = 0;
scanf("%d", &n);
int *people = new int[n], *catalan = new int[n];
for(int i = 0; i < n; i ++){
scanf("%d", &people[i]);
catalan[i] = i>=n/2?1:0;
}
sort(people, people+n);
int *firstRow = new int[n/2], *secondRow = new int[n/2];
do{
int *tmp_f = firstRow, *tmp_s = secondRow;
if(isCatalan(catalan, n)){
for(int i = 0; i < n; i ++){
if(catalan[i]==0) {
*tmp_f = people[i];
tmp_f ++;
}
if(catalan[i]==1) {
*tmp_s = people[i];
tmp_s ++;
}
}
for(int i = 0; i < n/2; i ++){
printf("%d ", firstRow[i]);
}
for(int i = 0; i < n/2; i ++){
printf("%d", secondRow[i]);
if(i<n/2-1) printf(" ");
}
printf("\n");
cnt ++;
}
} while(next_permutation(catalan, catalan+n));
printf("%d", cnt);
return 0;
}