Skip to content

Commit 612e1f7

Browse files
authored
Merge pull request #19 from KodaHye/main
[2주차] 고다혜
2 parents 167bbad + bb03e70 commit 612e1f7

File tree

8 files changed

+443
-78
lines changed

8 files changed

+443
-78
lines changed

.github/pull_request_template.md

Lines changed: 0 additions & 78 deletions
This file was deleted.

BOJ/1000-10000번/DH_1647.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
도시 분할 계획
6+
*/
7+
8+
public class DH_1647 {
9+
static class Edge implements Comparable<Edge> {
10+
int s, e, w;
11+
public Edge(int s, int e, int w) {
12+
this.s = s;
13+
this.e = e;
14+
this.w = w;
15+
}
16+
17+
@Override
18+
public int compareTo(Edge o) {
19+
return Integer.compare(this.w, o.w);
20+
}
21+
}
22+
23+
static PriorityQueue<Edge> pq;
24+
static int N, M, p[];
25+
static long result;
26+
27+
static void mst() {
28+
int cnt = 0;
29+
while(!pq.isEmpty() && cnt < N - 2) {
30+
Edge current = pq.poll();
31+
32+
int s = current.s;
33+
int e = current.e;
34+
35+
if(find(s) == find(e)) continue;
36+
union(s, e);
37+
cnt++;
38+
result += current.w;
39+
}
40+
41+
System.out.println(result);
42+
}
43+
44+
static void union(int a, int b) {
45+
a = find(a);
46+
b = find(b);
47+
48+
if(a != b) p[b] = a;
49+
}
50+
51+
static int find(int a) {
52+
return p[a] = a == p[a] ? a : find(p[a]);
53+
}
54+
55+
public static void main(String[] args) throws Exception {
56+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
57+
StringTokenizer st = new StringTokenizer(br.readLine());
58+
59+
N = Integer.parseInt(st.nextToken());
60+
M = Integer.parseInt(st.nextToken());
61+
62+
p = new int[N + 1];
63+
for(int i = 0; i < p.length; i++) p[i] = i;
64+
65+
pq = new PriorityQueue<>();
66+
for(int i = 0; i < M; i++) {
67+
st = new StringTokenizer(br.readLine());
68+
int a = Integer.parseInt(st.nextToken());
69+
int b = Integer.parseInt(st.nextToken());
70+
int c = Integer.parseInt(st.nextToken());
71+
72+
pq.add(new Edge(a, b, c));
73+
}
74+
75+
mst();
76+
}
77+
}

BOJ/1000-10000번/DH_2631.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* 줄세우기
3+
*/
4+
5+
public class DH_2631 {
6+
static int N;
7+
static int[] arr, cnt;
8+
9+
public static void main(String[] args) throws Exception {
10+
N = read();
11+
12+
arr = new int[N];
13+
cnt = new int[N];
14+
15+
int maxCnt = 0;
16+
for(int i = 0; i < N; i++) arr[i] = read();
17+
18+
for(int i = 0; i < N; i++) {
19+
cnt[i] = 1;
20+
21+
for(int j = 0; j < i; j++) {
22+
if(arr[i] < arr[j]) continue;
23+
if(cnt[i] < cnt[j] + 1) {
24+
cnt[i] = cnt[j] + 1;
25+
}
26+
}
27+
28+
maxCnt = Math.max(maxCnt, cnt[i]);
29+
}
30+
31+
System.out.println(N - maxCnt);
32+
}
33+
static int read() throws Exception {
34+
int c, n = System.in.read() & 15;
35+
while((c = System.in.read()) >= 48)
36+
n = (n << 3) + (n << 1) + (c & 15);
37+
if(c == 13) System.in.read();
38+
return n;
39+
}
40+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class DH_예술성 {
5+
static class Node {
6+
int num, cnt;
7+
public Node(int num, int cnt) {
8+
this.num = num;
9+
this.cnt = cnt;
10+
}
11+
}
12+
// map: 주어진 배열 정보
13+
// v: idx를 저장하는 배열
14+
// dis: idx영역끼리 얼마나 맞닿아 있는지 저장하는 배열
15+
static int result, N, map[][], v[][], dis[][];
16+
static int dr[] = {-1, 1, 0, 0}, dc[] = {0, 0, -1, 1};
17+
// 특정 영역의 정보 저장
18+
static HashMap<Integer, Node> hashMap;
19+
static Queue<int []> q;
20+
static Queue<int [][]> tmpQueue;
21+
22+
static void solution() {
23+
int turn = 0;
24+
while(turn < 4) {
25+
result += getScore();
26+
rotateCross();
27+
rotateElement();
28+
turn++;
29+
}
30+
31+
System.out.println(result);
32+
}
33+
34+
static void rotateElement() { // 십자 모양을 제외한 4개의 정사각형을 회전하는 함수
35+
int tmp[][] = new int[N / 2][N / 2]; // tmp라는 배열에 정사각형 원본 저장
36+
37+
// 시작점: (0, 0), (0, N / 2 + 1), (N / 2 + 1, 0), (N / 2 + 1, N / 2 + 1)
38+
for(int r = 0; r < N; r += (N / 2 + 1)) {
39+
for(int c = 0; c < N; c += (N / 2 + 1)) {
40+
for(int rr = r; rr < r + (N / 2); rr++) {
41+
System.arraycopy(map[rr], c, tmp[rr - r], 0, N / 2);
42+
}
43+
44+
for(int rr = 0; rr < N / 2; rr++) {
45+
for(int cc = 0; cc < N / 2; cc++) {
46+
// 배열 돌리기
47+
map[r + rr][c + cc] = tmp[N / 2 - 1 - cc][rr];
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
// 십자 회전
55+
static void rotateCross() {
56+
int tmp[]; // 원본 저장할 tmp 배열
57+
tmp = Arrays.copyOf(map[N / 2], N);
58+
for(int r = 0; r < N; r++) map[N / 2][r] = map[r][N / 2];
59+
for(int r = 0; r < N; r++) map[r][N / 2] = tmp[N - 1 - r];
60+
}
61+
62+
// 조화로움의 합 구하기
63+
static int getBeautyScore() {
64+
int result = 0;
65+
for(int idx1 = 1; idx1 < dis.length; idx1++) {
66+
for(int idx2 = idx1 + 1; idx2 < dis.length; idx2++) {
67+
int tmp;
68+
tmp = (hashMap.get(idx1).cnt + hashMap.get(idx2).cnt);
69+
tmp *= hashMap.get(idx1).num;
70+
tmp *= hashMap.get(idx2).num;
71+
tmp *= dis[idx1][idx2];
72+
73+
result += tmp;
74+
}
75+
}
76+
77+
return result;
78+
}
79+
static int getScore() {
80+
bfs();
81+
return getBeautyScore();
82+
}
83+
84+
static void bfs() {
85+
hashMap.clear();
86+
v = new int[N][N];
87+
88+
int idx = 1;
89+
90+
for(int r = 0; r < map.length; r++) {
91+
for(int c = 0; c < map[0].length; c++) {
92+
int cnt = 0;
93+
94+
if(v[r][c] != 0) continue;
95+
q.add(new int[] {r, c});
96+
cnt++;
97+
v[r][c] = idx;
98+
while(!q.isEmpty()) {
99+
int current[] = q.poll();
100+
101+
for(int d = 0; d < 4; d++) {
102+
int nr = current[0] + dr[d];
103+
int nc = current[1] + dc[d];
104+
105+
if(!check(nr, nc)) continue;
106+
if(v[nr][nc] != 0) {
107+
if(v[nr][nc] != v[r][c]) tmpQueue.add(new int[][] {{r, c}, {nr, nc}});
108+
continue;
109+
} else if(map[nr][nc] != map[r][c]) continue;
110+
q.add(new int[] {nr, nc});
111+
112+
v[nr][nc] = idx;
113+
cnt++;
114+
}
115+
}
116+
117+
hashMap.put(idx, new Node(map[r][c], cnt));
118+
119+
idx++;
120+
}
121+
}
122+
123+
dis = new int[idx][idx];
124+
125+
// dis: 맞닿아 있는 변의 수
126+
while(!tmpQueue.isEmpty()) {
127+
int current[][] = tmpQueue.poll();
128+
129+
int idx1 = v[current[0][0]][current[0][1]];
130+
int idx2 = v[current[1][0]][current[1][1]];
131+
dis[idx1][idx2]++;
132+
dis[idx2][idx1]++;
133+
}
134+
}
135+
136+
static boolean check(int r, int c) {
137+
return r >= 0 && r < N && c >= 0 && c < N;
138+
}
139+
public static void main(String[] args) throws Exception {
140+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
141+
StringTokenizer st;
142+
143+
N = Integer.parseInt(br.readLine());
144+
map = new int[N][N];
145+
hashMap = new HashMap<>();
146+
147+
q = new LinkedList<>();
148+
tmpQueue = new LinkedList<>();
149+
150+
for(int r = 0; r < map.length; r++) {
151+
st = new StringTokenizer(br.readLine());
152+
153+
for(int c = 0; c < map[0].length; c++) {
154+
map[r][c] = Integer.parseInt(st.nextToken());
155+
}
156+
}
157+
158+
solution();
159+
}
160+
}

Programmers/Level2/DH_12913.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Programmers.Level2;
2+
3+
/*
4+
땅따먹기
5+
*/
6+
7+
public class DH_12913 {
8+
9+
class Solution {
10+
int solution(int[][] land) {
11+
int answer = 0;
12+
13+
int R = land.length, C = land[0].length;
14+
int dp[][] = new int[R + 1][C];
15+
16+
for(int r = 1; r < R + 1; r++) {
17+
for(int c1 = 0; c1 < C; c1++) {
18+
for(int c2 = 0; c2 < C; c2++) {
19+
if(c1 == c2) continue;
20+
dp[r][c1] = Math.max(land[r - 1][c1] + dp[r - 1][c2], dp[r][c1]);
21+
answer = Math.max(answer, dp[r][c1]);
22+
}
23+
}
24+
}
25+
26+
return answer;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)