-
Notifications
You must be signed in to change notification settings - Fork 4
[2주차] 고다혜 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[2주차] 고다혜 #19
Changes from all commits
4f195b1
0920f0d
993db75
aa2433c
a2e8742
bc578c9
a39accf
bb03e70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
/* | ||
도시 분할 계획 | ||
*/ | ||
|
||
public class DH_1647 { | ||
static class Edge implements Comparable<Edge> { | ||
int s, e, w; | ||
public Edge(int s, int e, int w) { | ||
this.s = s; | ||
this.e = e; | ||
this.w = w; | ||
} | ||
|
||
@Override | ||
public int compareTo(Edge o) { | ||
return Integer.compare(this.w, o.w); | ||
} | ||
} | ||
|
||
static PriorityQueue<Edge> pq; | ||
static int N, M, p[]; | ||
static long result; | ||
|
||
static void mst() { | ||
int cnt = 0; | ||
while(!pq.isEmpty() && cnt < N - 2) { | ||
Edge current = pq.poll(); | ||
|
||
int s = current.s; | ||
int e = current.e; | ||
|
||
if(find(s) == find(e)) continue; | ||
union(s, e); | ||
cnt++; | ||
result += current.w; | ||
} | ||
|
||
System.out.println(result); | ||
} | ||
|
||
static void union(int a, int b) { | ||
a = find(a); | ||
b = find(b); | ||
|
||
if(a != b) p[b] = a; | ||
} | ||
|
||
static int find(int a) { | ||
return p[a] = a == p[a] ? a : find(p[a]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요거.. 흥미롭네요🫨 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ㅋㅎㅋㅎㅋ... 제가 생각하기에 삼항연산자 쓰면 직관적인거 같기도 하고,,,, 숏코딩하는거 좋아합니다,,, |
||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
N = Integer.parseInt(st.nextToken()); | ||
M = Integer.parseInt(st.nextToken()); | ||
|
||
p = new int[N + 1]; | ||
for(int i = 0; i < p.length; i++) p[i] = i; | ||
|
||
pq = new PriorityQueue<>(); | ||
for(int i = 0; i < M; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
int a = Integer.parseInt(st.nextToken()); | ||
int b = Integer.parseInt(st.nextToken()); | ||
int c = Integer.parseInt(st.nextToken()); | ||
|
||
pq.add(new Edge(a, b, c)); | ||
} | ||
|
||
mst(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 줄세우기 | ||
*/ | ||
|
||
public class DH_2631 { | ||
static int N; | ||
static int[] arr, cnt; | ||
|
||
public static void main(String[] args) throws Exception { | ||
N = read(); | ||
|
||
arr = new int[N]; | ||
cnt = new int[N]; | ||
|
||
int maxCnt = 0; | ||
for(int i = 0; i < N; i++) arr[i] = read(); | ||
|
||
for(int i = 0; i < N; i++) { | ||
cnt[i] = 1; | ||
|
||
for(int j = 0; j < i; j++) { | ||
if(arr[i] < arr[j]) continue; | ||
if(cnt[i] < cnt[j] + 1) { | ||
cnt[i] = cnt[j] + 1; | ||
} | ||
} | ||
|
||
maxCnt = Math.max(maxCnt, cnt[i]); | ||
} | ||
|
||
System.out.println(N - maxCnt); | ||
} | ||
static int read() throws Exception { | ||
int c, n = System.in.read() & 15; | ||
while((c = System.in.read()) >= 48) | ||
n = (n << 3) + (n << 1) + (c & 15); | ||
if(c == 13) System.in.read(); | ||
return n; | ||
Comment on lines
+33
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 빠른 입출력! 사용하셨군요👍🏻 확실히 빠릅니다요.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 따라해봤습니다!!! 진짜 확실히 빠르더라구요,,,, 마자요,,예술성 문제 풀 때, 입력이 이상하게 돼서,, 그냥 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class DH_예술성 { | ||
static class Node { | ||
int num, cnt; | ||
public Node(int num, int cnt) { | ||
this.num = num; | ||
this.cnt = cnt; | ||
} | ||
} | ||
// map: 주어진 배열 정보 | ||
// v: idx를 저장하는 배열 | ||
// dis: idx영역끼리 얼마나 맞닿아 있는지 저장하는 배열 | ||
static int result, N, map[][], v[][], dis[][]; | ||
static int dr[] = {-1, 1, 0, 0}, dc[] = {0, 0, -1, 1}; | ||
// 특정 영역의 정보 저장 | ||
static HashMap<Integer, Node> hashMap; | ||
static Queue<int []> q; | ||
static Queue<int [][]> tmpQueue; | ||
|
||
static void solution() { | ||
int turn = 0; | ||
while(turn < 4) { | ||
result += getScore(); | ||
rotateCross(); | ||
rotateElement(); | ||
turn++; | ||
} | ||
|
||
System.out.println(result); | ||
} | ||
|
||
static void rotateElement() { // 십자 모양을 제외한 4개의 정사각형을 회전하는 함수 | ||
int tmp[][] = new int[N / 2][N / 2]; // tmp라는 배열에 정사각형 원본 저장 | ||
|
||
// 시작점: (0, 0), (0, N / 2 + 1), (N / 2 + 1, 0), (N / 2 + 1, N / 2 + 1) | ||
for(int r = 0; r < N; r += (N / 2 + 1)) { | ||
for(int c = 0; c < N; c += (N / 2 + 1)) { | ||
for(int rr = r; rr < r + (N / 2); rr++) { | ||
System.arraycopy(map[rr], c, tmp[rr - r], 0, N / 2); | ||
} | ||
|
||
for(int rr = 0; rr < N / 2; rr++) { | ||
for(int cc = 0; cc < N / 2; cc++) { | ||
// 배열 돌리기 | ||
map[r + rr][c + cc] = tmp[N / 2 - 1 - cc][rr]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 십자 회전 | ||
static void rotateCross() { | ||
int tmp[]; // 원본 저장할 tmp 배열 | ||
tmp = Arrays.copyOf(map[N / 2], N); | ||
for(int r = 0; r < N; r++) map[N / 2][r] = map[r][N / 2]; | ||
for(int r = 0; r < N; r++) map[r][N / 2] = tmp[N - 1 - r]; | ||
} | ||
|
||
// 조화로움의 합 구하기 | ||
static int getBeautyScore() { | ||
int result = 0; | ||
for(int idx1 = 1; idx1 < dis.length; idx1++) { | ||
for(int idx2 = idx1 + 1; idx2 < dis.length; idx2++) { | ||
int tmp; | ||
tmp = (hashMap.get(idx1).cnt + hashMap.get(idx2).cnt); | ||
tmp *= hashMap.get(idx1).num; | ||
tmp *= hashMap.get(idx2).num; | ||
tmp *= dis[idx1][idx2]; | ||
|
||
result += tmp; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
static int getScore() { | ||
bfs(); | ||
return getBeautyScore(); | ||
} | ||
|
||
static void bfs() { | ||
hashMap.clear(); | ||
v = new int[N][N]; | ||
|
||
int idx = 1; | ||
|
||
for(int r = 0; r < map.length; r++) { | ||
for(int c = 0; c < map[0].length; c++) { | ||
int cnt = 0; | ||
|
||
if(v[r][c] != 0) continue; | ||
q.add(new int[] {r, c}); | ||
cnt++; | ||
v[r][c] = idx; | ||
while(!q.isEmpty()) { | ||
int current[] = q.poll(); | ||
|
||
for(int d = 0; d < 4; d++) { | ||
int nr = current[0] + dr[d]; | ||
int nc = current[1] + dc[d]; | ||
|
||
if(!check(nr, nc)) continue; | ||
if(v[nr][nc] != 0) { | ||
if(v[nr][nc] != v[r][c]) tmpQueue.add(new int[][] {{r, c}, {nr, nc}}); | ||
continue; | ||
} else if(map[nr][nc] != map[r][c]) continue; | ||
q.add(new int[] {nr, nc}); | ||
|
||
v[nr][nc] = idx; | ||
cnt++; | ||
} | ||
} | ||
|
||
hashMap.put(idx, new Node(map[r][c], cnt)); | ||
|
||
idx++; | ||
} | ||
} | ||
|
||
dis = new int[idx][idx]; | ||
|
||
// dis: 맞닿아 있는 변의 수 | ||
while(!tmpQueue.isEmpty()) { | ||
int current[][] = tmpQueue.poll(); | ||
|
||
int idx1 = v[current[0][0]][current[0][1]]; | ||
int idx2 = v[current[1][0]][current[1][1]]; | ||
dis[idx1][idx2]++; | ||
dis[idx2][idx1]++; | ||
} | ||
} | ||
|
||
static boolean check(int r, int c) { | ||
return r >= 0 && r < N && c >= 0 && c < N; | ||
} | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st; | ||
|
||
N = Integer.parseInt(br.readLine()); | ||
map = new int[N][N]; | ||
hashMap = new HashMap<>(); | ||
|
||
q = new LinkedList<>(); | ||
tmpQueue = new LinkedList<>(); | ||
|
||
for(int r = 0; r < map.length; r++) { | ||
st = new StringTokenizer(br.readLine()); | ||
|
||
for(int c = 0; c < map[0].length; c++) { | ||
map[r][c] = Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
|
||
solution(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package Programmers.Level2; | ||
|
||
/* | ||
땅따먹기 | ||
*/ | ||
|
||
public class DH_12913 { | ||
|
||
class Solution { | ||
int solution(int[][] land) { | ||
int answer = 0; | ||
|
||
int R = land.length, C = land[0].length; | ||
int dp[][] = new int[R + 1][C]; | ||
|
||
for(int r = 1; r < R + 1; r++) { | ||
for(int c1 = 0; c1 < C; c1++) { | ||
for(int c2 = 0; c2 < C; c2++) { | ||
if(c1 == c2) continue; | ||
dp[r][c1] = Math.max(land[r - 1][c1] + dp[r - 1][c2], dp[r][c1]); | ||
answer = Math.max(answer, dp[r][c1]); | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
간선 비용이 더 작은 것을 우선순위 큐로 출력하는 것이 최적화된 좋은 방법인거 같아요! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
동감합니다!! cnt < N-2조건으로 최대비용 간선 처리를 쉽게 한것도 멋있어요!