Skip to content

[김동하-11주차 알고리즘 스터디] #75

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions 김동하/11주차/[백준 1562] 계단 수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static StringBuilder sb = new StringBuilder();

public static int n;
public static long[][][] dp;
public static long ans;

public static final long MOD = 1000000000;

public static void init() throws IOException{
n = Integer.parseInt(br.readLine());
dp = new long[n + 1][10][1<<10];
ans = 0;
}

public static void solution() throws IOException{
init();
for(int i = 1; i < 10; i++) {
dp[1][i][1 << i] = 1;
}
// i = 자릿수, j = 마지막 수, k = 포함 수
/**
* i = 3, j = 5, k = 1000100000
* i = 4, j =
*/
for(int i = 2; i <= n; i++) {
for(int j = 0; j < 10; j++) {
for(int k = 0; k < (1 << 10); k++) {
int used = (1 << j) | k;
if(j == 0) dp[i][j][used] = (dp[i][j][used] + dp[i - 1][j + 1][k]) % MOD;
else if(j == 9) dp[i][j][used] = (dp[i][j][used] + dp[i - 1][j - 1][k]) % MOD;
else {
dp[i][j][used] = (dp[i][j][used] + (dp[i - 1][j - 1][k] + dp[i - 1][j + 1][k]) % MOD) % MOD;
}
}
}
}
for(int i = 0; i < 10; i++) {
ans += dp[n][i][1023];
ans %= MOD;
}
}

public static void main(String[] args) throws IOException{
solution();
sb.append(ans);
bw.append(sb.toString());
bw.flush();
bw.close();
br.close();
}
}
111 changes: 111 additions & 0 deletions 김동하/11주차/[백준 17490] 일감호에 다리 놓기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.io.*;
import java.util.*;

public class Main {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static StringBuilder sb = new StringBuilder();

public static int n, m;
public static long k;
public static PriorityQueue<Node> edges;
public static long ans = 0L;

public static class Node implements Comparable<Node> {
int x, y;
int dist;

public Node(int x, int y, int dist) {
this.x = x;
this.y = y;
this.dist = dist;
}

@Override
public int compareTo(Node o) {
return this.dist - o.dist;
}
}

public static int[] parents;

public static void init() throws IOException {
StringTokenizer stk = new StringTokenizer(br.readLine());
n = Integer.parseInt(stk.nextToken());
m = Integer.parseInt(stk.nextToken());
k = Long.parseLong(stk.nextToken());
edges = new PriorityQueue<>();
stk = new StringTokenizer(br.readLine());
parents = new int[n + 1];
for (int i = 1; i <= n; i++) {
edges.add(new Node(0, i, Integer.parseInt(stk.nextToken())));
parents[i] = i;
}
boolean connectedRight[] = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
connectedRight[i] = true;
}
for (int i = 0; i < m; i++) {
stk = new StringTokenizer(br.readLine());
int x = Integer.parseInt(stk.nextToken());
int y = Integer.parseInt(stk.nextToken());
int a = Math.min(x, y);
int b = Math.max(x, y);
if(b == n && a == 1) connectedRight[b] = false;
else connectedRight[a] = false;
}
for (int i = 1; i <= n; i++) {
if (!connectedRight[i]) continue;
if (i == n) union(1, n);
else union(i, i + 1);
}
}

public static int find(int x) {
if (parents[x] == x) return parents[x];
else return parents[x] = find(parents[x]);
}

public static boolean union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return false;
if (x < y) parents[y] = x;
else parents[x] = y;
return true;
}


public static boolean isAllUnion() {
int x = find(1);
for (int i = 2; i <= n; i++) {
if (x != find(i)) return false;
}
return true;
}

public static void solution() throws IOException {
init();
if (m <= 1) sb.append("YES");
else {
while (!edges.isEmpty()) {
Node cur = edges.poll();
if (ans > k) break;
boolean flag = union(cur.x, cur.y);
if (flag) {
ans += cur.dist;
}
}
if (isAllUnion() && ans <= k) sb.append("YES");
else sb.append("NO");
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}

public static void main(String[] args) throws IOException {
solution();
}
}
Loading