File tree Expand file tree Collapse file tree 4 files changed +256
-0
lines changed Expand file tree Collapse file tree 4 files changed +256
-0
lines changed Original file line number Diff line number Diff line change
1
+ package combinatorics ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ public class BOJ_1722_순열의순서 {
6
+
7
+ static int N ;
8
+ static int [] comb ;
9
+ static long [] f ;
10
+
11
+ public static void main (String [] args ) {
12
+ Scanner sc = new Scanner (System .in );
13
+
14
+ N = sc .nextInt ();
15
+
16
+ // 팩토리얼 구하기
17
+ f = new long [21 ];
18
+ f [0 ] = 1 ; // 0!은 0이 아니라 1임에 유의!!
19
+ for (int i = 1 ; i <= 20 ; i ++) {
20
+ f [i ] = f [i -1 ] * i ;
21
+ }
22
+
23
+ comb = new int [N ];
24
+ int type = sc .nextInt ();
25
+ if (type == 1 ) {
26
+ long idx = sc .nextLong ();
27
+ sol1 (idx );
28
+ } else {
29
+ for (int i = 0 ; i < N ; i ++) {
30
+ comb [i ] = sc .nextInt ();
31
+ }
32
+
33
+ sol2 (comb );
34
+ }
35
+ }
36
+
37
+ static void sol1 (long idx ) {
38
+ boolean [] visited = new boolean [21 ];
39
+ for (int i = 0 ; i < N ; i ++) {
40
+ for (int j = 1 ; j <= N ; j ++) {
41
+ if (visited [j ]) continue ;
42
+
43
+ if (f [N -i -1 ] < idx ) {
44
+ idx -= f [N -i -1 ];
45
+ } else {
46
+ comb [i ] = j ;
47
+ visited [j ] = true ;
48
+ break ;
49
+ }
50
+ }
51
+ }
52
+
53
+ for (int i = 0 ; i < N ; i ++) {
54
+ System .out .print (comb [i ]+" " );
55
+ }
56
+ }
57
+
58
+ static void sol2 (int [] comb ) {
59
+ long idx = 1 ;
60
+ boolean [] visited = new boolean [21 ];
61
+
62
+ for (int i = 0 ; i < N ; i ++) {
63
+ for (int j = 1 ; j < comb [i ]; j ++) {
64
+ if (!visited [j ]) {
65
+ idx += f [N -i -1 ];
66
+ }
67
+ }
68
+ visited [comb [i ]] = true ;
69
+ }
70
+
71
+ System .out .println (idx );
72
+
73
+ Math .atan (360 );
74
+ }
75
+
76
+ }
Original file line number Diff line number Diff line change
1
+ package combinatorics ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .Enumeration ;
5
+ import java .util .Scanner ;
6
+
7
+ /**
8
+ * 암호는 최소 1개의 모음, 2개의 자음으로 이루어져있다
9
+ * 암호는 알파벳 순서대로 나열되어야 한다
10
+ */
11
+ public class BOJ_1759_암호만들기 {
12
+
13
+ static int L , C ;
14
+ static String [] chars ;
15
+ static String [] output ;
16
+ static StringBuilder sb = new StringBuilder ();
17
+
18
+ public static void main (String [] args ) {
19
+ Scanner sc = new Scanner (System .in );
20
+
21
+ L = sc .nextInt ();
22
+ C = sc .nextInt ();
23
+
24
+ chars = new String [C ];
25
+ for (int i = 0 ; i < C ; i ++) {
26
+ chars [i ] = sc .next ();
27
+ }
28
+ output = new String [L ];
29
+
30
+ solution ();
31
+ System .out .println (sb );
32
+ }
33
+
34
+ static void solution () {
35
+ Arrays .sort (chars );
36
+ comb (0 , 0 );
37
+ }
38
+
39
+ static void comb (int depth , int start ) {
40
+ if (depth == L ) {
41
+ if (check ()) {
42
+ for (int i = 0 ; i < output .length ; i ++) {
43
+ sb .append (output [i ]);
44
+ }
45
+ sb .append ('\n' );
46
+ }
47
+ return ;
48
+ }
49
+
50
+ for (int i = start ; i < chars .length ; i ++) {
51
+ output [depth ] = chars [i ];
52
+ comb (depth +1 , i +1 );
53
+ }
54
+ }
55
+
56
+ // 최소 모음, 자음 갯수를 충족하면 true 반환
57
+ static boolean check () {
58
+ int cCnt = 0 ; // 자음 갯수
59
+ int vCnt = 0 ; // 모음 갯수
60
+
61
+ String vowel = "aeiou" ;
62
+ for (int i = 0 ; i < output .length ; i ++) {
63
+ if (vowel .contains (output [i ])) {
64
+ vCnt ++;
65
+ } else {
66
+ cCnt ++;
67
+ }
68
+ }
69
+
70
+ if (cCnt >= 2 && vCnt >= 1 ) {
71
+ return true ;
72
+ }
73
+ return false ;
74
+ }
75
+
76
+ }
Original file line number Diff line number Diff line change
1
+ package dp ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ /**
6
+ * 파스칼의 삼각형 공식 활용
7
+ * nCr = (n-1)C(r-1) + (n-1)C(r)
8
+ *
9
+ * dp 배열의 정의
10
+ * dp[i][j] = iCj의 값
11
+ */
12
+ public class BOJ_11051_이항계수2 {
13
+
14
+ static int [][] dp ;
15
+ static final int MOD = 10_007 ;
16
+
17
+ public static void main (String [] args ) {
18
+ Scanner sc = new Scanner (System .in );
19
+
20
+ int N = sc .nextInt ();
21
+ int K = sc .nextInt ();
22
+
23
+ dp = new int [N +1 ][N +1 ];
24
+
25
+ System .out .println (solution (N , K ));
26
+ }
27
+
28
+ static int solution (int n , int r ) {
29
+ if (n == r || r == 0 ) {
30
+ return 1 ;
31
+ }
32
+
33
+ if (dp [n ][r ] == 0 ) {
34
+ dp [n ][r ] = solution (n -1 , r -1 ) + solution (n -1 , r );
35
+ dp [n ][r ] %= MOD ;
36
+ }
37
+ return dp [n ][r ];
38
+ }
39
+
40
+ }
Original file line number Diff line number Diff line change
1
+ package dp ;
2
+
3
+ import java .io .BufferedReader ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStreamReader ;
6
+ import java .util .Arrays ;
7
+
8
+ public class BOJ_11054_가장긴바이토닉부분수열 {
9
+
10
+ static int N ;
11
+ static int [] nums ;
12
+ static int [] inc_dp ;
13
+ static int [] dec_dp ;
14
+
15
+ public static void main (String [] args ) throws IOException {
16
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
17
+
18
+ N = Integer .parseInt (br .readLine ());
19
+ nums = Arrays .stream (br .readLine ().split (" " )).mapToInt (Integer ::parseInt ).toArray ();
20
+ inc_dp = new int [N ];
21
+ dec_dp = new int [N ];
22
+
23
+ System .out .println (solution ());
24
+ }
25
+
26
+ static int solution () {
27
+ LIS ();
28
+ LDS ();
29
+
30
+ int max = 0 ;
31
+ for (int i = 0 ; i < N ; i ++) {
32
+ max = Math .max (max , inc_dp [i ] + dec_dp [i ]);
33
+ }
34
+
35
+ return max -1 ; // 바이토닉 수열의 정점 부분이 중복 카운트되므로 -1 처리
36
+ }
37
+
38
+ // 가장 긴 증가하는 부분수열
39
+ static void LIS () {
40
+ for (int i = 0 ; i < N ; i ++) {
41
+ inc_dp [i ] = 1 ; // 1로 초기화
42
+
43
+ for (int j = 0 ; j < i ; j ++) {
44
+ if (nums [j ] < nums [i ]) {
45
+ inc_dp [i ] = Math .max (inc_dp [i ], inc_dp [j ]+1 );
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ // 가장 긴 감소하는 부분수열
52
+ static void LDS () {
53
+ for (int i = N -1 ; i >= 0 ; i --) {
54
+ dec_dp [i ] = 1 ;
55
+
56
+ for (int j = N -1 ; j > i ; j --) {
57
+ if (nums [j ] < nums [i ]) {
58
+ dec_dp [i ] = Math .max (dec_dp [i ], dec_dp [j ]+1 );
59
+ }
60
+ }
61
+ }
62
+ }
63
+
64
+ }
You can’t perform that action at this time.
0 commit comments