diff --git a/doa/BOJ1202.java b/doa/BOJ1202.java new file mode 100644 index 0000000..13ea11f --- /dev/null +++ b/doa/BOJ1202.java @@ -0,0 +1,80 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Scanner; + +public class BOJ1202 { + + static class jewl implements Comparable{ + int W, V; + + public jewl(int W, int V) { + super(); + this.W = W; + this.V = V; + } + + + @Override + public int compareTo(jewl o) { + // TODO Auto-generated method stub + return this.W - o.W; + } + + + } + + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int K = sc.nextInt(); + + List jewls= new ArrayList<>(); + + PriorityQueue bags= new PriorityQueue(); + PriorityQueue bestV= new PriorityQueue(Collections.reverseOrder()); + + for (int i = 0; i < N; i++) { + jewls.add(new jewl(sc.nextInt(),sc.nextInt())); + } + + Collections.sort(jewls); + + for (int i = 0; i < K; i++) { + bags.add(sc.nextInt()); + } + + + long answer=0; + //value 순으로 정렬, 하고, + //bag는 낮은 순으로 정렬, + //낮은 순서대로 value를 뽑을 때 bag 기준만 충족하면 뽑아 + + int idx=0; + for(int i=0 ;i< K ; i++ ) { + + int nowB = bags.poll(); + + while(idx { + int problem, difficulty, count; + + public prob(int problem, int difficulty) { + super(); + this.problem = problem; + this.difficulty = difficulty; + } + + @Override + public int compareTo(prob o) { + // 난이도가 높은 것부터 먼저 오도록 + if (this.difficulty != o.difficulty) { + return Integer.compare(this.difficulty, o.difficulty); + } + // 난이도가 같을 경우 문제 번호가 큰 것 우선 + return this.problem - o.problem; + } + + } + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + + PriorityQueue low = new PriorityQueue(); + PriorityQueue high = new PriorityQueue(Collections.reverseOrder()); + Map map = new HashMap<>(); + + for (int i = 0; i < N; i++) { + int P = sc.nextInt(); + int L = sc.nextInt(); + low.add(new prob(P, L)); + high.add(new prob(P, L)); + map.put(P,L); + } + + N = sc.nextInt(); + + for (int i = 0; i < N; i++) { + String commend = sc.next(); + + if (commend.equals("recommend")) { + int num = sc.nextInt(); + if (num == 1) { + while(!high.isEmpty()) { + prob top = high.peek(); + if (map.containsKey(top.problem) && map.get(top.problem) == top.difficulty) { + System.out.println(top.problem); + break; + } + high.poll(); + } + + + } else { + + while(!low.isEmpty()) { + prob top = low.peek(); + if (map.containsKey(top.problem) && map.get(top.problem) == top.difficulty) { + System.out.println(top.problem); + break; + } + low.poll(); + } + + + + } + } else if (commend.equals("solved")) { + int num = sc.nextInt(); + map.remove(num); + + } + //추가할 경우 + else { + int P = sc.nextInt(); + int L = sc.nextInt(); + low.add(new prob(P, L)); + high.add(new prob(P, L)); + map.put(P,L); + } + + } + + } + + +} diff --git a/doa/BOJ5639.java b/doa/BOJ5639.java new file mode 100644 index 0000000..0c4e5bd --- /dev/null +++ b/doa/BOJ5639.java @@ -0,0 +1,69 @@ +package 알고리즘; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.List; + +public class BOJ5639 { + + static class Node { + int key; + Node left, right; + + Node(int key) { + this.key = key; + left = right = null; + } + } + + static Node insertNode(Node node, int num) { + + if(node == null) + return new Node(num); + + if(num < node.key) { + node.left = insertNode(node.left, num); + }else { + + node.right = insertNode(node.right, num); + } + + return node; + + } + + static void postOrder(Node num) { + + if(num!=null) { + postOrder(num.left); + postOrder(num.right); + System.out.println(num.key); + } + + } + + public static void main(String args[]) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String input; + + + //루트 값 입력 + Node now = null; + + while((input = br.readLine()) != null && !input.isEmpty()) { + int num = Integer.parseInt(input); + + now = insertNode(now,num); + + } + + + postOrder(now); + + } + +} + + + diff --git a/doa/boj1043.java b/doa/boj1043.java new file mode 100644 index 0000000..13fe6f4 --- /dev/null +++ b/doa/boj1043.java @@ -0,0 +1,92 @@ +package 알고리즘; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Scanner; +import java.util.Set; + +public class boj1043 { + + static int M; + static int N; + static Set cantLie; + static List> party; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + party = new ArrayList<>(); + + N = sc.nextInt(); + M = sc.nextInt(); + + int N1 = sc.nextInt(); + cantLie = new HashSet<>(); + + for(int i=0;i parties = new HashSet<>(); + for(int j=0;j queue = new LinkedList<>(); + + //특정 수만큼 + boolean[] visitedPeople = new boolean[N+1]; + boolean[] visitedParty = new boolean[M+1]; + + for(int a : cantLie) { + visitedPeople[a]=true; + } + + queue.addAll(cantLie); + + while(!queue.isEmpty()) { + + int now = queue.poll(); + int idx =0; + + for(Set a : party) { + idx++; + + if(visitedParty[idx]) continue; + + if(a.contains(now)) { + count++; + visitedParty[idx]=true; + cantLie.addAll(a); + + for(int c : a) { + if(!visitedPeople[c]) { + queue.add(c); + visitedPeople[c]=true; + } + } + } + } + + } + + return count; + } + + } + diff --git a/doa/boj1197.java b/doa/boj1197.java new file mode 100644 index 0000000..df183d0 --- /dev/null +++ b/doa/boj1197.java @@ -0,0 +1,86 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class boj1197 { + + static int[] parents; + static List graphs ; + static int E; + + static class graph implements Comparable{ + int one, two, weight; + + public graph(int one, int two, int weight) { + super(); + this.one = one; + this.two = two; + this.weight = weight; + } + + @Override + public int compareTo(graph o) { + // TODO Auto-generated method stub + return this.weight-o.weight; + } + + } + static int answer=0; + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + + int V = sc.nextInt(); + E = sc.nextInt(); + + graphs = new ArrayList<>(); + parents = new int[V+1]; + + + for(int i=1; i list = new ArrayList<>(); + + + for(int i=0 ;i< N ; i++) { + arr[i] = sc.nextInt(); + } + + + list.add(arr[0]); + +for(int i=1 ;i arr; + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + + N = sc.nextLong(); + P = sc.nextInt(); + Q = sc.nextInt(); + + arr = new HashMap<>(); + arr.put(0L, 1L); + findAn(N); + System.out.println(arr.get(N)); + + } + public static Long findAn(long n) { + + if(arr.containsKey(n)) + return arr.get(n); + + long result = findAn(n / P) + findAn(n / Q); + arr.put(n, result); + + return result; + + } + + +} diff --git a/doa/boj13975.java b/doa/boj13975.java new file mode 100644 index 0000000..7cc713b --- /dev/null +++ b/doa/boj13975.java @@ -0,0 +1,36 @@ +import java.util.PriorityQueue; +import java.util.Scanner; + +public class boj13975 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int T = sc.nextInt(); + + for(int tc=0 ;tc < T ; tc++) { + int N = sc.nextInt(); + PriorityQueue cards = new PriorityQueue<>(); + + for(int i=0; i where = new PriorityQueue<>((o1,o2)-> o1-o2); + + + for(int i=0 ;i< N ; i++) { + where.add(sc.nextInt()); + } + + where.add(L); + + int[] distance = new int[N+1]; + + int first=0; + + int second=0; + + int max =0; + for(int i=0; i< N+1 ; i++) { + second = where.poll(); + distance[i] = second - first; + max = Math.max(max, (second-first)); + first = second; + } + + int left=1; + int right=max; + int answer =0; + + while(left<=right) { + + int mid = (left+right)/2; + + int count=N; + + for(int i : distance) { + + if(i/mid >= 1) { + + if(i%mid==0) count+= (i/mid)-1; + else{ + count+= (i/mid); + } + + } + } + + if(count > (M+N)){ + left = mid+1; + }else { + right = mid-1; + answer = mid; + } + + } + + System.out.println(answer); + + + } + +} diff --git a/doa/boj1655.java b/doa/boj1655.java new file mode 100644 index 0000000..b0659b9 --- /dev/null +++ b/doa/boj1655.java @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.PriorityQueue; + +public class boj1655 { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + int N = Integer.parseInt(br.readLine()); + + PriorityQueue left = new PriorityQueue<>(Collections.reverseOrder()); + PriorityQueue right = new PriorityQueue<>(); + + // 첫 번째 값 추가 + int now = Integer.parseInt(br.readLine()); + left.add(now); + sb.append(now).append("\n"); // 첫 번째 출력 + + for (int i = 1; i < N; i++) { // 1부터 N-1까지 반복 + now = Integer.parseInt(br.readLine()); + + // 값 비교 후 추가 + if (now <= left.peek()) { + left.add(now); + } else { + right.add(now); + } + + // 사이즈 맞추기 + if (left.size() > right.size() + 1) { + right.add(left.poll()); + } else if (right.size() > left.size()) { + left.add(right.poll()); + } + + // 출력 + sb.append(left.peek()).append("\n"); + } + + // 최종 출력 + System.out.print(sb.toString()); + br.close(); + } +} diff --git a/doa/boj1707.java b/doa/boj1707.java new file mode 100644 index 0000000..453d5fa --- /dev/null +++ b/doa/boj1707.java @@ -0,0 +1,87 @@ +package 알고리즘; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Scanner; + +public class boj1707 { + + static int V; + static List> graph; + static int[] color; + static String answer; + + public static void main(String[] args) throws IOException { + Scanner sc = new Scanner(System.in); + + int T = sc.nextInt(); + + for (int tc = 0; tc < T; tc++) { + V = sc.nextInt(); + int E = sc.nextInt(); + answer = "YES"; + graph = new ArrayList<>(); + color = new int[V+1]; + + for (int i = 0; i <= V; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < E; i++) { + int A = sc.nextInt(); + int B = sc.nextInt(); + + graph.get(A).add(B); + graph.get(B).add(A); + } + + bfs(); + + System.out.println(answer); + } + } + public static void bfs() { + + Queue queue = new LinkedList<>(); + queue.add(1); + + + for(int i=1; i<=V ;i++) { + + if(queue.isEmpty()) { + for(int j=2; j<=V ;j++) { + if(color[j]==0) { + queue.add(j); + break; + } + } + + } + + int now = queue.poll(); + if(color[now]==0) color[now]=1; + + for(int a : graph.get(now)) { + + if(color[a]==color[now]) { + answer ="NO"; + return; + } + + if(color[a]==0 &&color[now]==1 ) { + color[a]=2; + queue.add(a); + } + else if(color[a]==0 &&color[now]==2) { + color[a]=1; + queue.add(a); + } + + } + } + } + + +} diff --git a/doa/boj1715.java b/doa/boj1715.java new file mode 100644 index 0000000..2e8688f --- /dev/null +++ b/doa/boj1715.java @@ -0,0 +1,33 @@ +import java.util.PriorityQueue; +import java.util.Scanner; + +public class boj1715 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + PriorityQueue cards = new PriorityQueue<>(); + + for(int i=0; i hs; + static int N, M, K; + static char[][] map; + static String[] word; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + N = sc.nextInt();// 세러 + M = sc.nextInt();// 가로 + K = sc.nextInt(); // 문자열 개수 + + map = new char[N][M]; // + for (int i = 0; i < N; i++) { + String s = sc.next(); + for (int j = 0; j < M; j++) { + map[i][j] = s.charAt(j); + } + } // 입력 + + hs = new HashMap<>(); + + word = new String[K];// 순서대로 나중에 출력하기 위해서 배열로 + + for (int i = 0; i < K; i++) { + word[i] = sc.next(); + hs.put(word[i], 0);// 해쉬에다가 신이 좋아하는 문자열 넣고 value는 0으로 넣음 + } + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + dfs(i, j, ""); //초기에는 빈문자열에서 부터 시작하니까 + } + } + + for (int i = 0; i < K; i++) { + System.out.println(hs.get(word[i])); + + } // 해쉬맵에 있는 value값을 출력한다. + + }// main + + static void dfs(int i, int j, String currword) { // 좌표와 hs인덱스 값 + if (currword.length() >= 5) { // 문자열 길이가 5이상이면 종료 + return; + } + + currword += map[i][j];// 문자 하나씩 추가해보기 + + if (hs.containsKey(currword)) { + hs.put(currword, hs.get(currword) + 1); // 갯수 세야하니까 value에다가 1더하기 + } + + int[] dr = { -1, 1, 0, 0, -1, -1, 1, 1 }; + int[] dc = { 0, 0, -1, 1, -1, 1, 1, -1 }; // 상하좌우 대각선 8방 + + for (int d = 0; d < 8; d++) { + + int nr = (i + dr[d] + N) % N; // 경계 처리 + int nc = (j + dc[d] + M) % M; // 경계 처리 + + dfs(nr, nc, currword); //dfs 진행 + + } + + } + +} diff --git a/doa/boj2295.java b/doa/boj2295.java new file mode 100644 index 0000000..8d86d24 --- /dev/null +++ b/doa/boj2295.java @@ -0,0 +1,77 @@ +import java.util.Arrays; +import java.util.PriorityQueue; +import java.util.Scanner; + +public class Main { + + static int N; + static int[] nums; + + static int ans; + static boolean flag=false; + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + + N = sc.nextInt(); + + PriorityQueue num = new PriorityQueue<>((o1,o2)-> o2-o1); + + for(int i=0 ;i< N ; i++) { + num.add(sc.nextInt()); + } + + nums = new int[N]; + for(int i=0 ;i< N ; i++) { + nums[i]= num.poll(); + + } + + ans=-1; + + for(int i=0 ;i = 0) { + find(i, count+1, sum); + } + sum +=nums[i]; + if(count==2) { + + } + + } + + +} + +} diff --git a/doa/boj2617.java b/doa/boj2617.java new file mode 100644 index 0000000..758dc2e --- /dev/null +++ b/doa/boj2617.java @@ -0,0 +1,81 @@ +package 알고리즘; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Scanner; + +public class boj2617 { + + static int V; + //가벼운게 0 무거운게 1 + static List> lighter; + static List> heavyer; + static int[] color; + static String answer; + + public static void main(String[] args) throws IOException { + Scanner sc = new Scanner(System.in); + + V = sc.nextInt(); + int E = sc.nextInt(); + lighter = new ArrayList<>(); + heavyer = new ArrayList<>(); + // Initialize the adjacency list + + int standard = V/2; + + for (int i = 0; i <= V; i++) { + lighter.add(new ArrayList<>()); + heavyer.add(new ArrayList<>()); + } + for (int i = 0; i < E; i++) { + int A = sc.nextInt(); + int B = sc.nextInt(); + + lighter.get(A).add(B); + heavyer.get(B).add(A); + //나보다 가벼운 애를 담기 + + } + + + int answer =0; + for(int i=1 ; i<=V; i++) { + + // i보다 가벼운 애들을 담기 + if(bfs(i, lighter)>standard) answer ++; + if(bfs(i, heavyer)>standard) answer ++; + //i보다 무거운 애들의 수를 담기 + } + + System.out.println(answer); + } + + public static int bfs(int i, List> list) { + + int count=0; + Queue queue = new LinkedList<>(); + boolean visited[] = new boolean[V+1]; + visited[i]=true; + queue.add(i); + + while(!queue.isEmpty()) { + int now = queue.poll(); + + for(int a : list.get(now)) { + if(!visited[a]) { + queue.add(a); + count++; + visited[a]=true; + } + } + + } + + return count; + + } + +} diff --git a/doa/boj2792.java b/doa/boj2792.java new file mode 100644 index 0000000..86a41b8 --- /dev/null +++ b/doa/boj2792.java @@ -0,0 +1,51 @@ +import java.util.Scanner; + +public class boj2792 { + + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + + //아이들의 수 N과 색상의 수 M + int N = sc.nextInt(); + int M = sc.nextInt(); + + int[] jewl = new int[M]; + + long max =0; + for(int i=0 ;i hipertube; + static Queue queue = new LinkedList<>(); + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + + N = sc.nextInt(); + K = sc.nextInt(); + M = sc.nextInt(); + int[][] arr = new int[K][M]; + hipertube= new ArrayList<>(); + + + for(int i =0 ;i list = new HashSet<>(); + for(int j=0 ; j a : hipertube) { + idx++; + + if(visitedConnection[idx]) continue; + + if(a.contains(now)) { + + visitedConnection[idx]=true; + + for(int c : a) { + if(!visitedStation[c]) { + if(c==1) return ++count; + queue.add(c); + visitedStation[c]=true; + } + } + } + } + + } + + count++; + } + + return -1; + } + + } +