- Today
- Total
- PS
- μΈν΄
- leetcode
- java
- μμμ λ ¬
- μ‘Έμ μν
- λ°±μλ
- λ°μ΄ν°λ² μ΄μ€
- λ€μ΅μ€νΈλΌ
- λ°±μ€
- 벨λ§ν¬λ
- μλ°
- CS
- spring
- μλ°μμ μ
- MST
- λ¬Έλ²
- νλ‘κ·Έλλ¨Έμ€
- dp
- database
- OOP
- Algorithm
- Graph
- pytorch
- 그리λ
- μλ£κ΅¬μ‘°
- ꡬν
- BFS
- array
- tree
Partially Committed
[λ°±μ€ 13549/1956/1620/2629] μλ° λ³Έλ¬Έ
(title: [λ°±μ€ 13549/1956/1620/2629] μλ°)
A. μ¨λ°κΌμ§3 BOJ 13549
https://www.acmicpc.net/problem/13549
λμ΄λ | Gold 5 |
νμ΄ μκ° | 20 λΆ |
λΆλ₯ | λ€μ΅μ€νΈλΌ / 0-1 BFS |
μκ°λ³΅μ‘λ | λ€μ΅μ€νΈλΌ : O(nlogn) 0-1 BFS : O(V+E) = O(N+N) ~= O(N) |
곡κ°λ³΅μ‘λ |
μΌμ°¨μ κ·Έλνμμ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μννμ¬ μ½κ² ν΄κ²°ν μ μλ€.
λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μ¬μ©νλ μ΄μ λ λ¬Έμ μν©μ 1μ°¨μ κ·Έλνλ‘ λ°κΎΈμ΄ μκ°ν΄λ³΄λ©΄
λͺ¨λ κ°μ€μΉλ 1 λλ 0 μΌλ‘ μμ κ°μ μ κ°μ§λ κ²μ μ μ μκ³ ,
λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μ¬μ©νλ©΄ O(nlogn) ~= 500000 μ΄κΈ° λλ¬Έμ μκ° μ νμλ 걸릴 μΌλ €κ° μκΈ° λλ¬Έμ΄λ€. μκ°μ΄λμ νλ κ²½μ°μλ κ°μ€μΉκ° 0 μΈ λ Έλλ₯Ό μ°μ μμ νμ λ£μ΄μ£Όκ³ , κ±Έμ΄κ°λ κ²½μ°μλ κ°μ€μΉκ° 1 μΈ λ Έλλ₯Ό μ°μ μμ νμ λ£μ΄μ£Όλ©΄ λλ€.
[λ€μ΅μ€νΈλΌ]
import java.io.*;
import java.util.*;
public class Main {
static class node implements Comparable<node> {
int to;
int w;
public node(int to, int w) {
this.to = to;
this.w = w;
}
@Override
public int compareTo(node v) {
if (this.w > v.w)
return 1;
else return -1;
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stk.nextToken());
int k = Integer.parseInt(stk.nextToken());
int[] dist = new int[100001];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[n] = 0;
PriorityQueue<node> pq = new PriorityQueue<>();
pq.add(new node(n, 0));
while(!pq.isEmpty()){
node now = pq.poll();
node [] next = {
new node(now.to-1, 1),
new node(now.to+1, 1),
new node(now.to*2, 0)
};
for(node cur : next){
if(cur.to<0||cur.to>100000) continue; // out of bounds
if(dist[cur.to] > dist[now.to] + cur.w){
dist[cur.to] = dist[now.to] + cur.w;
pq.add(new node(cur.to, dist[cur.to]));
}
}
}
bw.write(dist[k] + "\n");
bw.flush();
bw.close();
}
}
νΉμ 0-1 BFS μκ³ λ¦¬μ¦μ μ¬μ©νλ©΄ λ³΄λ€ ν¨μ¨μ μΌ μ¬μ§κ° μλ€. 0-1 BFS λ 0 λλ 1 μ κ°μ€μΉλ§ κ°μ§λ κ·Έλνμμ μ΅λ¨κ²½λ‘λ₯Ό μ°Ύμλ΄λ μκ³ λ¦¬μ¦μΌλ‘ dequeue λ₯Ό μ¬μ©νμ¬, μ°μ μμμ λ°λΌ μμ add ν μ§, λ€μ add ν μ§λ₯Ό κ²°μ νλ©΄ λλ€. μ΄λ¬ν 0-1 BFS μκ³ λ¦¬μ¦μ O(V+E) μ μκ°λ³΅μ‘λλ₯Ό κ°μ§λ€.
[0-1 BFS]
import java.io.*;
import java.util.*;
public class Main {
static class node {
int to;
int w;
public node(int to, int w) {
this.to = to;
this.w = w;
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stk.nextToken());
int k = Integer.parseInt(stk.nextToken());
int[] dist = new int[100001];
boolean[] visited = new boolean[100001];
Arrays.fill(dist, -1);
dist[n] = 0;
visited[n] = true;
LinkedList<node> dq = new LinkedList<>();
dq.add(new node(n, 0));
while (!dq.isEmpty()) {
node now = dq.poll();
node[] next = {
new node(now.to * 2, 0),
new node(now.to - 1, 1),
new node(now.to + 1, 1)
};
for (node cur : next) {
if (cur.to < 0 || cur.to > 100000) continue; // out of bounds
if (!visited[cur.to]) {
visited[cur.to] = true;
if (cur.w == 0) { // μκ°μ΄λ
dist[cur.to] = dist[now.to];
dq.addFirst(new node(cur.to, cur.w));
} else { // κ±Έμ΄κ°
dist[cur.to] = dist[now.to] + cur.w;
dq.addLast(new node(cur.to, cur.w));
}
}
}
}
bw.write(dist[k] + "\n");
bw.flush();
bw.close();
}
}
B. μ΄λ BOJ 1956
https://www.acmicpc.net/problem/1956
λμ΄λ | Gold 4 |
νμ΄ μκ° | 20 λΆ |
λΆλ₯ | νλ‘μ΄λ - μμ |
μκ°λ³΅μ‘λ | O(V^3) ~= O(400^3) = O(64000000) |
곡κ°λ³΅μ‘λ |
κ°κ°μ λ Έλμμ λλ¬ν μ μλ μ΅λ¨ κ²½λ‘ ν μ΄λΈμ΄ μλ€λ©΄, λ¬Έμ μμ μ μνλ 'μ¬μ΄ν΄' μ μ½κ² νλ¨ν μ μλ€. μλ₯Ό λ€μ΄, a->b λ‘ κ°λ μ΅λ¨ κ²½λ‘κ° μ‘΄μ¬νλ€λ©΄ b->a λ‘ κ°λ μ΅λ¨ κ²½λ‘κ° μ‘΄μ¬νλ μ§ νμΈνκ³ , λ§μ½ μ‘΄μ¬νλ€λ©΄ κ·Έ λμ μ΅λ¨ κ²½λ‘μ ν©μ΄ μ¬μ΄ν΄ κ±°λ¦¬κ° λ κ²μ΄λ€. κ·Έ λ€μμλ a->c λ‘ κ°λ μ΅λ¨ κ²½λ‘κ° μ‘΄μ¬νλ€λ©΄ c->a λ‘ κ°λ μ΅λ¨ κ²½λ‘κ° μ‘΄μ¬νλ μ§ νμΈν΄λ³΄κ³ λ μμ κ°μΌλ‘ κ³μ κ°±μ ν΄λκ°λ©΄ λ κ² μ΄λ€.
μ¦, λͺ¨λ λ Έλ κ° μ΅λ¨ κ²½λ‘κ° μμΌλ©΄ μ’μν λ° λ¬Έμ μ μ ν μ¬νμ 보λ λ Έλμ κ°μλ μ΅λ 400 κ° μ΄λ€. κ·Έλ¬λ©΄, λͺ¨λ λ Έλ κ° μ΅λ¨ κ²½λ‘λ₯Ό μ»λ μκ³ λ¦¬μ¦μΈ νλ‘μ΄λ-μμ μκ³ λ¦¬μ¦μ μννμ¬λ μκ° μ ν λ΄μ ν΅κ³Όν μ μμ κ²μμ μ μ μλ€.(O(V^3))
κ·Έλν 거리 μ 보λ₯Ό 2μ°¨μ νλ ¬μ λ°μ λ€μ, ν루μ΄λ μμ μκ³ λ¦¬μ¦μ μ μ©νλ€. κ·Έλ¦¬κ³ λμ A->B κΉμ§μ μ΅λ¨ κ²½λ‘κ° μλ€λ©΄ B->A κ²½λ‘κ° μλμ§ νμΈνκ³ μλ€λ©΄ μ΄ λ κ²½λ‘μ ν©μ μ μ₯νλ€. μ΄λ¬ν κ³Όμ μ λͺ¨λ λ Έλμ λνμ¬ λ°λ³΅νλ©° μ΅λ¨ μ¬μ΄ν΄μ μΆλ ₯ν΄μ£Όλ©΄ λλ€.
μ‘°μ¬ν΄μΌ ν μ μ, μ΅λ¨κ²½λ‘λ₯Ό ꡬνλ κ³Όμ μμ μ€λ²νλ‘μ°κ° λ°μνμ¬ μ°λ¦¬κ° μνμ§ μλ κ°μΌλ‘ ν μ΄λΈμ΄ μ±μμ§ μ μλ€λ κ²μ΄λ€. λ νλλ, μ£Όμ΄μ§ κ·Έλνμμ μ¬μ΄ν΄μ΄ λ°μνμ§ μλ κ²½μ°μλ -1 μ μΆλ ₯ν΄μΌ νλ€λ κ²μ μ‘°μ¬ν΄μΌ νλ€.
import java.io.*;
import java.util.*;
public class Main {
static int v;
static int e;
static long[][] dist;
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
v = Integer.parseInt(stk.nextToken());
e = Integer.parseInt(stk.nextToken());
dist = new long[v + 1][v + 1];
for (int i = 1; i <= v; i++) {
for (int j = 1; j <= v; j++) {
if (i == j)
dist[i][j] = 0;
else
dist[i][j] = Integer.MAX_VALUE;
}
}
for (int i = 0; i < e; i++) {
stk = new StringTokenizer(br.readLine());
int a = Integer.parseInt(stk.nextToken());
int b = Integer.parseInt(stk.nextToken());
int cost = Integer.parseInt(stk.nextToken());
dist[a][b] = cost;
}
for (int k = 1; k <= v; k++) {
for (int st = 1; st <= v; st++) {
for (int end = 1; end <= v; end++) {
if (dist[st][end] > dist[st][k] + dist[k][end]) {
dist[st][end] = dist[st][k] + dist[k][end];
}
}
}
}
long min = Integer.MAX_VALUE;
for (int i = 1; i <= v; i++) {
for (int j = 1; j <= v; j++) {
if(i==j) continue;
if(dist[i][j] != Integer.MAX_VALUE && dist[j][i] != Integer.MAX_VALUE){
min = Math.min(dist[i][j] + dist[j][i] ,min);
}
}
}
if(min==Integer.MAX_VALUE){
bw.write("-1\n");
} else {
bw.write(min+"\n");
}
bw.flush();
bw.close();
}
}
C. λλμΌ ν¬μΌλͺ¬ λ§μ€ν° μ΄λ€μ BOJ 1620
https://www.acmicpc.net/problem/1620
λμ΄λ | Silver 4 |
νμ΄ μκ° | 10λΆ |
λΆλ₯ | ν΄μ¬λ§΅ |
μκ°λ³΅μ‘λ | |
곡κ°λ³΅μ‘λ |
ν΄μ¬λ§΅μ μ¬μ©ν μ€ μλμ§ λ¬Όμ΄λ³΄λ λ¬Έμ μ΄λ€!
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stk.nextToken());
int m = Integer.parseInt(stk.nextToken());
int cnt = 1;
HashMap<Integer, String> num_name_Map = new HashMap<>();
HashMap<String, Integer> name_num_Map = new HashMap<>();
for (int i = 0; i < n; i++) {
String input = br.readLine();
num_name_Map.put(cnt, input);
name_num_Map.put(input, cnt);
cnt++;
}
for (int i = 0; i < m; i++) {
String input = br.readLine();
char first = input.charAt(0);
boolean isitnum = true;
if('A'<=first && first<='Z'){
isitnum = false;
}
if(isitnum){
// num
bw.write(num_name_Map.get(Integer.valueOf(input))+"\n");
} else {
// name
bw.write(name_num_Map.get(input)+"\n");
}
}
bw.flush();
bw.close();
}
}
java hash_map
JAVA μ ν΄μ¬λ§΅μ array μ linked-list λ‘ μ΄λ£¨μ΄μ§.
κ·Έλμ KEY κ°μ μλ€λ©΄ O(1) μμ ARRAY μμ κΊΌλ΄μ¬ μ μλ κ²μ.
λ§μ½ Collision μ΄ λ°μνλ€λ©΄ Linked-LIst Chaining μ μνν¨.
get, put, remove, containsKey : O(1) ~ O(n)
containsValue : O(n)
Collision μ΄ λ§μ΄ λ°μνμ¬ Linked-List κ° λ무 κΈΈμ΄μ§λ©΄ μ±λ₯μ κ°μ νκΈ° μν΄ JAVA 8 λΆν°λ Tree κ΅¬μ‘°λ‘ λ³κ²½νλ€κ³ ν¨.
D. μν μ μΈ BOJ 2629
https://www.acmicpc.net/problem/2629
λμ΄λ | Gold 3 |
νμ΄ μκ° | νμ΄κ° μλ μ¬λΌμ ꡬκΈλ§ν¨..π’ |
λΆλ₯ | λ°±νΈλνΉ, DP |
μκ°λ³΅μ‘λ | |
곡κ°λ³΅μ‘λ |
νκ·Έλ₯Ό μ΄μ΄λ³΄λ λ μ λ¬Έμ λΌκ³ λμ΄ μμ΄μ, μ΄λ₯Ό κ³ λ €νλ©΄μ νλ €κ³ νμ§λ§
κ·Έλ₯ λ°±νΈλνΉμ λλ©΄μ dp λ₯Ό μ±μ°λ λ°©μμ΄ μ‘°κΈ λ μ§κ΄μ μΈ κ² κ°λ€
dp[idx][w] ; idx κΉμ§μ μΆλ₯Ό νμΈνμμ λ w λΌλ 무κ²λ₯Ό λ§λ€ μ μλμ§ μ¬λΆ
λΌκ³ μ μνμ.
κ·Έλ¦¬κ³ idx λ²μ§Έμ μΆλ₯Ό νμΈνλ©΄μ μ°λ¦¬κ° ν μ μλ λμμ μλμ 3 κ°μ§λ§ κ°λ₯νλ€.
1. ꡬμ¬μ λ§μνΈμ μΆλ₯Ό μ¬λ €λλλ€.
βΆμ΄λ¬ν κ²½μ°μλ w+weight[idx] 무κ²λ₯Ό νμΈν μ μκ² λλ€.
2. ꡬμ¬κ³Ό κ°μ νΈμ μΆλ₯Ό μ¬λ €λλλ€.
βΆμ΄λ¬ν κ²½μ°μλ Math.abs( w-weight[idx] ) 무κ²λ₯Ό νμΈν μ μκ² λλ€.
3. ν΄λΉ μΆλ₯Ό μ¬λ €λμ§ μλλ€.
βΆμ΄λ¬ν κ²½μ°μλ w 무κ²λ₯Ό νμΈν μ μκ² λλ€.
μ΄λ₯Ό μ¬κ·ν¨μ ννλ‘ κ΅¬ννλ©΄ λλ€.
import java.io.*;
import java.util.*;
public class Main {
static boolean [][] dp;
static int n;
static int [] weight;
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
n = Integer.parseInt(br.readLine()); // μΆμ κ°μ
weight = new int[n+1];
stk = new StringTokenizer(br.readLine());
int total = 0;
for(int i = 0 ; i < n; i++){
weight[i] = Integer.parseInt(stk.nextToken());
total += weight[i];
}
dp = new boolean[n+1][15001];
solve(0,0);
int m = Integer.parseInt(br.readLine()); // 쿼리 κ°μ
stk = new StringTokenizer(br.readLine());
for(int i = 0 ; i < m; i ++){
int target = Integer.parseInt(stk.nextToken());
if(target > 500*30)
bw.write("N ");
else if(dp[n][target])
bw.write("Y ");
else bw.write("N ");
}
bw.flush();
bw.close();
}
static void solve(int idx, int w){
if(idx>n)
return;
if(dp[idx][w])
return;
dp[idx][w] = true;
solve(idx+1, w + weight[idx]); // ꡬμ¬μ λ§μ νΈμ μΆλ₯Ό μ¬λ¦¬λ κ²½μ°
solve(idx+1, w); // μΆλ₯Ό μ¬λ¦¬μ§ μλ κ²½μ°
solve(idx+1, Math.abs(w-weight[idx])); // ꡬμ¬κ³Ό κ°μ νΈμ μΆλ₯Ό μ¬λ¦¬λ κ²½μ°
}
}
'π₯ Algorithm || λ¬Έμ νμ΄ > PS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Union and Find] λ°±μ€ 20040 μ¬μ΄ν΄ κ²μ (0) | 2023.03.07 |
---|---|
[Meet in the middle] λ°±μ€ 1450 λ μ λ¬Έμ (0) | 2023.03.06 |
[νλ‘κ·Έλλ¨Έμ€] μΉ΄λλμΉ (JAVA) (1) | 2023.02.20 |
[λ°±μ€ 9251/1520/9370] μλ° (0) | 2023.02.20 |
[λ°±μ€ 5568/2559/1504/11066] μλ° (0) | 2023.02.19 |