Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a8ab2e

Browse files
authored
Merge pull request #170 from GreatAlgorithm-Study/dahye
[12주차] 고다혜
2 parents ff02d52 + efb7ed4 commit 9a8ab2e

File tree

9 files changed

+538
-0
lines changed

9 files changed

+538
-0
lines changed

‎BOJ/1000-5000번/DH_2352.java‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 반도체 설계
6+
*/
7+
8+
public class DH_2352 {
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st;
12+
13+
int n = Integer.parseInt(br.readLine());
14+
15+
int[] arr = new int[n];
16+
int[] dp = new int[n];
17+
18+
st = new StringTokenizer(br.readLine());
19+
for(int i = 0; i < n; i++) arr[i] = Integer.parseInt(st.nextToken());
20+
21+
// int result = getResultByDP(arr, dp);
22+
int result = getResultByBinarySearch(arr);
23+
24+
System.out.println(result);
25+
}
26+
27+
// 이분탐색을 통해 LIS의 길이 구하기
28+
static int getResultByBinarySearch(int[] arr) {
29+
int[] lis = new int[arr.length];
30+
31+
int cnt = 1;
32+
33+
lis[0] = arr[0];
34+
35+
for(int i = 1; i < arr.length; i++) {
36+
if(lis[cnt - 1] < arr[i]) lis[cnt++] = arr[i];
37+
else {
38+
int idx = binarySearch(lis, arr[i], 0, cnt);
39+
lis[idx] = arr[i];
40+
}
41+
}
42+
43+
return cnt;
44+
}
45+
46+
// lower bound 구하기
47+
static int binarySearch(int[] lis, int value, int s, int e) {
48+
while(s <= e) {
49+
int m = (s + e) / 2;
50+
if(lis[m] < value) s = m + 1;
51+
else e = m - 1;
52+
}
53+
return s;
54+
}
55+
56+
// 단순 DP를 통해 LIS 구하기
57+
static int getResultByDP(int[] arr, int[] dp) {
58+
int result = 0;
59+
60+
for(int i = 0; i < arr.length; i++) {
61+
dp[i] = 1;
62+
63+
for(int j = 0; j < i; j++) {
64+
if(dp[i] < dp[j] + 1 && arr[j] < arr[i]) {
65+
dp[i] = dp[j] + 1;
66+
}
67+
}
68+
69+
result = Math.max(result, dp[i]);
70+
}
71+
72+
return result;
73+
}
74+
}

‎BOJ/1000-5000번/DH_2533.java‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 사회망 서비스(SNS)
6+
*/
7+
8+
public class DH_2533 {
9+
static int N;
10+
static ArrayList<Integer>[] adj;
11+
static Queue<Integer> q;
12+
13+
public static void main(String[] args) throws Exception {
14+
initInput();
15+
solution();
16+
}
17+
18+
static void solution() {
19+
// 얼리 어답터가 아닌 사람은 자신의 모든 친구들이 얼리 어답터일 때만 아이디어를 받아들임
20+
// 리프노트 구해주기
21+
getLeafNode();
22+
System.out.println(getEarlyAdopter());
23+
}
24+
25+
static int getEarlyAdopter() {
26+
int answer = 0;
27+
28+
boolean[] v = new boolean[N + 1];
29+
30+
while(!q.isEmpty()) {
31+
int current = q.poll();
32+
33+
// 리프노드는 그냥 확인해줬다는 표시만 해주기
34+
if(v[current]) continue;
35+
v[current] = true;
36+
37+
// 리프노드와 이어져 있는 노드(부모) 찾기
38+
for(int i = 0; i < adj[current].size(); i++) {
39+
int next = adj[current].get(i); // next: 부모노드
40+
41+
if(v[next]) continue;
42+
// 리프노드의 부모 노드는 무조건 얼리어답터가 됨
43+
v[next] = true;
44+
answer += 1;
45+
46+
// 부모노드(얼리어답터)에 이어져 있는 노드의 간선 모두 없애기 (부모 노드 기준 들어오는거 없애기)
47+
for(int j = 0; j < adj[next].size(); j++) {
48+
int check = adj[next].get(j);
49+
if(v[check]) continue;
50+
51+
int removeIdx = adj[check].indexOf(next);
52+
adj[check].remove(removeIdx);
53+
54+
// 부모 노드 기준 들어오는 간선의 시작점이 리프노드가 되었나면 리프노드 큐에 넣어주기
55+
if(adj[check].size() == 1) q.add(check);
56+
}
57+
}
58+
}
59+
60+
return answer;
61+
}
62+
63+
// 리프노트 큐에 넣어주기
64+
static void getLeafNode() {
65+
q = new ArrayDeque<Integer>();
66+
for(int i = 1; i < adj.length; i++) {
67+
if(adj[i].size() == 1) q.add(i);
68+
}
69+
}
70+
71+
static void initInput() throws Exception {
72+
System.setIn(new FileInputStream("./input/BOJ2533.txt"));
73+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
74+
StringTokenizer st;
75+
76+
N = Integer.parseInt(br.readLine());
77+
adj = new ArrayList[N + 1];
78+
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Integer>();
79+
80+
for(int i = 0; i < N - 1; i++) {
81+
st = new StringTokenizer(br.readLine());
82+
int a = Integer.parseInt(st.nextToken());
83+
int b = Integer.parseInt(st.nextToken());
84+
85+
adj[a].add(b);
86+
adj[b].add(a);
87+
}
88+
}
89+
}

‎BOJ/30000-35000번/DH_28707.java‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 배열 정렬
6+
*/
7+
8+
public class DH_28707 {
9+
static final int INF = Integer.MAX_VALUE;
10+
static class Node implements Comparable<Node> {
11+
int[] arr;
12+
int c;
13+
public Node(int[] arr, int c) {
14+
this.arr = arr;
15+
this.c = c;
16+
}
17+
18+
@Override
19+
public int compareTo(Node o) {
20+
return Integer.compare(this.c, o.c);
21+
}
22+
}
23+
static class Calculator {
24+
int l, r, c;
25+
public Calculator(int l, int r, int c) {
26+
this.l = l;
27+
this.r = r;
28+
this.c = c;
29+
}
30+
}
31+
static int N, M;
32+
static int[] A; // 초기 입력되는 배열
33+
static HashMap<String, Integer> cost; // key: Arrays.toString(int[] arr), value: int[] arr을 만들때까지 비용
34+
static ArrayList<Calculator> cal; // 조작에 대한 정보 저장
35+
36+
public static void main(String[] args) throws Exception {
37+
initInput();
38+
solution();
39+
}
40+
41+
static void solution() {
42+
PriorityQueue<Node> pq = new PriorityQueue();
43+
cost = new HashMap();
44+
45+
pq.add(new Node(A.clone(), 0));
46+
cost.put(Arrays.toString(A), 0);
47+
Arrays.sort(A); // A의 비내림차순 결과
48+
String key = Arrays.toString(A);
49+
50+
while(!pq.isEmpty()) {
51+
Node current = pq.poll();
52+
if(A.equals(Arrays.toString(current.arr))) break;
53+
54+
for(Calculator ca: cal) {
55+
int[] copyArr = current.arr.clone();
56+
int l = ca.l, r = ca.r, c = ca.c;
57+
58+
int tmp = copyArr[l];
59+
copyArr[l] = copyArr[r];
60+
copyArr[r] = tmp;
61+
62+
String nextKey = Arrays.toString(copyArr);
63+
64+
int nextCost = current.c + c;
65+
if(!cost.containsKey(nextKey)) cost.put(nextKey, INF);
66+
if(nextCost < cost.get(nextKey)) {
67+
cost.put(nextKey, nextCost);
68+
pq.add(new Node(copyArr, nextCost));
69+
}
70+
}
71+
}
72+
73+
74+
// cost의 키에 A의 비내림차순 결과가 있으면 비용 get하고, 아니라면 -1 출력
75+
System.out.println(cost.containsKey(key) ? cost.get(key) : -1);
76+
}
77+
78+
static void initInput() throws Exception {
79+
System.setIn(new FileInputStream("./input/BOJ28707.txt"));
80+
81+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
82+
StringTokenizer st;
83+
84+
N = Integer.parseInt(br.readLine());
85+
A = new int[N];
86+
87+
st = new StringTokenizer(br.readLine());
88+
for(int i = 0; i < A.length; i++) A[i] = Integer.parseInt(st.nextToken());
89+
90+
M = Integer.parseInt(br.readLine());
91+
92+
// 조작 list 저장
93+
cal = new ArrayList();
94+
for(int i = 0; i < M; i++) {
95+
st = new StringTokenizer(br.readLine());
96+
int l = Integer.parseInt(st.nextToken()) - 1;
97+
int r = Integer.parseInt(st.nextToken()) - 1;
98+
int c = Integer.parseInt(st.nextToken());
99+
cal.add(new Calculator(l, r, c));
100+
}
101+
}
102+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /