Skip to content

[권혁준-15주차 알고리즘 스터디] #70

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 6 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
98 changes: 98 additions & 0 deletions 권혁준_15주차/[BOJ-16681] 등산.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
```java

import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static final long INF = (long)1e18 + 7;

static int N, M, D, E;
static List<int[]>[] V;
static int[] H;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
D = nextInt();
E = nextInt();

H = new int[N+1];
for(int i=1;i<=N;i++) H[i] = nextInt();

V = new List[N+1];
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
for(int i=0;i<M;i++) {
int a = nextInt(), b = nextInt(), c = nextInt();
V[a].add(new int[] {b,c});
V[b].add(new int[] {a,c});
}

}

static void solve() throws Exception {

long[] A = dijk(1), B = dijk(N);

long ans = -INF;
for(int i=2;i<N;i++) if(A[i] != INF && B[i] != INF) {
ans = Math.max(ans, (long)H[i]*E - D*(A[i]+B[i]));
}

if(ans == -INF) bw.write("Impossible");
else bw.write(ans + "\n");

}

static long[] dijk(int start) {
long[] X = new long[N+1];
Arrays.fill(X, INF);
X[start] = 0;
PriorityQueue<long[]> Q = new PriorityQueue<>((a,b) -> Long.compare(a[0], b[0]));
Q.offer(new long[] {0,start});
while(!Q.isEmpty()) {
long[] now = Q.poll();
long d = now[0];
int n = (int)now[1];
if(d > X[n]) continue;
for(int[] e:V[n]) {
int i = e[0], c = e[1];
if(X[i] > d+c && H[n] < H[i]) {
X[i] = d+c;
Q.offer(new long[] {X[i],i});
}
}
}
return X;
}

}

```
91 changes: 91 additions & 0 deletions 권혁준_15주차/[BOJ-1800] 인터넷 설치.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static final int INF = (int)1e9 + 7;

static int N, P, K;
static List<int[]>[] V;
static int[][] D;
static PriorityQueue<int[]> Q;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
P = nextInt();
K = nextInt();
V = new List[N+1];
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
for(int i=0;i<P;i++) {
int a = nextInt(), b = nextInt(), c = nextInt();
V[a].add(new int[] {b,c});
V[b].add(new int[] {a,c});
}

D = new int[N+1][K+1];
for(int i=1;i<=N;i++) Arrays.fill(D[i], INF);
Q = new PriorityQueue<>((a,b) -> a[0]-b[0]);

}

static void solve() throws Exception {

D[N][0] = 0;
Q.offer(new int[] {0,N,0});
int ans = INF;
while(!Q.isEmpty()) {
int[] now = Q.poll();
int d = now[0], n = now[1], k = now[2];
if(d > D[n][k]) continue;
if(n == 1) {
ans = Math.min(ans, d);
break;
}
for(int[] e:V[n]) {
int i = e[0], c = Math.max(d, e[1]);
// 안 쓰는 경우
if(D[i][k] > c) {
D[i][k] = c;
Q.offer(new int[] {c,i,k});
}
// 쓰는 경우
if(k<K && D[i][k+1] > d) {
D[i][k+1] = d;
Q.offer(new int[] {d,i,k+1});
}
}
}
bw.write((ans == INF ? -1 : ans) + "\n");

}

}
```
75 changes: 75 additions & 0 deletions 권혁준_15주차/[BOJ-18119] 단어 암기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M;
static int[] A, O;
static int[] T;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
A = new int[N];
T = new int[135];
int c = 0;
for(char i='a';i<='z';i++) {
if(i=='a' || i=='e' || i=='i' || i=='o' || i=='u') T[i] = -1;
else T[i] = c++;
}

for(int i=0;i<N;i++) {
for(char j:br.readLine().toCharArray()) if(T[j] != -1) A[i] |= (1<<T[j]);
}

O = new int[N];
for(int i=0;i<N;i++) O[i] = A[i];

}

static void solve() throws Exception {

int ans = N;
for(int i=0;i<M;i++) {
int o = nextInt();
char a = nextToken().charAt(0);
for(int j=0;j<N;j++) if((O[j] & (1<<T[a])) != 0) {
if(A[j] == O[j]) ans--;
A[j] ^= (1<<T[a]);
if(A[j] == O[j]) ans++;
}
bw.write(ans + "\n");
}

}

}
```
64 changes: 64 additions & 0 deletions 권혁준_15주차/[BOJ-1911] 흙길 보수하기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
```java

import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, L;
static int[][] A;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
L = nextInt();
A = new int[N][2];
for(int i=0;i<N;i++) for(int j=0;j<2;j++) A[i][j] = nextInt();

}

static void solve() throws Exception {

Arrays.sort(A, (a,b) -> a[0]-b[0]);
int ans = 0, next = -1;
for(int i=0;i<N;i++) {
int s = A[i][0], e = A[i][1];
if(next >= e) continue;
next = Math.max(s, next);
int temp = (e-next-1)/L + 1;
ans += temp;
next += temp*L;
}
bw.write(ans + "\n");

}

}

```
Loading