Skip to content
81 changes: 81 additions & 0 deletions jinhyun/BOJ/Main_BJ_1049_기타줄.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;

public class Main_BJ_1049_기타줄 {

public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

int ans = 1000;

st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // 끊어진 줄 수
int M = Integer.parseInt(st.nextToken()); // 브랜드 수

List<int[]> list = new ArrayList<>();

for(int i = 0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int six = Integer.parseInt(st.nextToken());
int one = Integer.parseInt(st.nextToken());
list.add(new int[]{six, one});
}

// 6개 세트 기준으로 정렬
Collections.sort(list, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] < o2[0]){
return -1;
}else if (o1[0]> o2[0]){
return 1;
}
return 0;
}
});


// 6개 패키지로만 수량 채우는 방법
if(N <= 6 ) {
ans = list.get(0)[0];
}else {
ans = N%6==0 ? list.get(0)[0] * (N / 6) : list.get(0)[0] * (N / 6 + 1);
}

int temp = 0;
// 6개 패키지로 일정부분 채우고 낱개로 일정부분 채우는 방법
temp = list.get(0)[0] * (N/6);
// 낱개 기준으로 정렬
Collections.sort(list, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[1] < o2[1]){
return -1;
}else if (o1[1]> o2[1]){
return 1;
}
return 0;
}
});
temp += (N%6) * list.get(0)[1];
if (ans > temp) {
ans = temp;
}

// 낱개로만 수량 채우는 방법
temp = N * list.get(0)[1];
if(ans > temp) {
ans = temp;
}


System.out.print(ans);
}

}
75 changes: 75 additions & 0 deletions jinhyun/BOJ/Main_BJ_10971_외판원_순회2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main_BJ_10971_외판원_순회2 {

static boolean[] isVisited;
static int[] pick;
static int[][] W;
static int N;
static int ans;
public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine()); // 도시 수
isVisited = new boolean[N]; // 방문 체크
pick = new int[N]; // 선택한 수열(순열)
W = new int[N][N]; // cost 입력
ans = Integer.MAX_VALUE; // 최저값 구하기 위해서 최대값으로 초기화

for(int i = 0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j<N; j++) {
W[i][j] = Integer.parseInt(st.nextToken());
}
}

perm(0);

System.out.println(ans);

}//main

// 순열 생성
static void perm(int cnt) {
if(cnt == N) {
calc();
return;
}

for(int i =0; i<N; i++) {
if(isVisited[i]) continue;
isVisited[i] = true;
pick[cnt] = i;
perm(cnt+1);
isVisited[i]= false;
}
}// perm

// cost 계산 및 최저 cost일 때 갱신
static void calc() {
int cost = 0;
for(int i = 0; i<N; i++) {
if(i==N-1) { // 마지막에 처음 도시 방문
int temp = W[pick[i]][pick[0]];
if(temp == 0) return;
cost += temp;
}else { // 다음 도시 방문
int temp = W[pick[i]][pick[i+1]];
if(temp == 0) return;
cost += temp;
}
}

if(ans > cost) {
ans = cost;
}

}// calc

}


72 changes: 72 additions & 0 deletions jinhyun/BOJ/Main_BJ_1342_행운의_문자열.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main_BJ_1342_행운의_문자열 {

static boolean[] isVisited;
static char[] ans;
static int[] arr;
static int totalCnt;

public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String s = br.readLine();
arr = new int[26];
isVisited = new boolean[s.length()];
ans = new char[s.length()];
totalCnt = 0;
perm(0, 0, s);

for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a']++;
}

//팩토리얼로 나눠주어 생성된 문자열의 중복을 정확히 없앨 수 있음 (aabbbaa)의 경우 144개 -> 1개
//set으로 풀이하면 메모리 초과 발생
for (int i = 0; i < 26; i++) {
if (arr[i] > 1)
totalCnt /= factorial(arr[i]);
}

System.out.println(totalCnt);
}

static void perm(int cnt, int flag, String s) {
if(cnt == s.length()) {
if(isVerified()) {
totalCnt++;
}
return;
}

for(int i = 0; i<s.length(); i++) {
if((flag & 1 << i) != 0) continue;

isVisited[i] = true;
ans[cnt]=s.charAt(i);
perm(cnt+1, flag|1<<i, s);
isVisited[i] = false;
}

}

static boolean isVerified() {
for(int i = 1; i<ans.length; i++) {
if(ans[i-1] == ans[i]) {
return false;
}
if(i<ans.length-1 && ans[i+1] == ans[i]) {
return false;
}
}
return true;
}

static int factorial(int N) {
if(N==1) {
return 1;
}
return N * factorial(N-1);
}
}
Loading