Skip to content

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

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
80 changes: 80 additions & 0 deletions 권혁준_13주차/[BOJ-10266] 시계 사진들.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
```java

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

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;
static int[] A, B;
static int[] X;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
A = new int[2*N-1];
B = new int[N];
X = new int[N];
int[] ta = new int[N];
int[] tb = new int[N];
for(int i=0;i<N;i++) ta[i] = nextInt();
for(int i=0;i<N;i++) tb[i] = nextInt();
Arrays.sort(ta);
Arrays.sort(tb);
for(int i=1;i<N;i++) A[i-1] = ta[i] - ta[i-1];
A[N-1] = 360000+ta[0] - ta[N-1];
for(int i=N;i<2*N-1;i++) A[i] = A[i%N];
for(int i=1;i<N;i++) B[i-1] = tb[i] - tb[i-1];
B[N-1] = 360000+tb[0] - tb[N-1];

}

static void solve() throws Exception{

for(int i=1;i<N;i++){
X[i] = X[i-1];
while(X[i]>0 && B[X[i]] != B[i]) X[i] = X[X[i]-1];
if(B[X[i]] == B[i]) X[i]++;
}

for(int i=0,j=0;i<A.length;i++){
while(j>0 && B[j] != A[i]) j = X[j-1];
if(B[j] == A[i]) j++;
if(j == N) {
bw.write("possible");
return;
}
}
bw.write("impossible");

}

}

```
106 changes: 106 additions & 0 deletions 권혁준_13주차/[BOJ-1507] 궁금한 민호.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
```java

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


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;
static List<int[]> E;
static int[] r;
static int[][] V;

static final int INF = 123456789;

static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
E = new ArrayList<>();
r = new int[N];
V = new int[N][N];
for(int i=0;i<N;i++) {
r[i] = i;
for(int j=0;j<N;j++) {
int a = nextInt();
if(j>i) E.add(new int[] {i,j,a});
}
}

}

static void solve() throws Exception{

int ans = 0, cnt = 0;
Collections.sort(E, (a,b) -> a[2]-b[2]);

for(int[] e:E) {
int a = e[0], b = e[1], c = e[2];
int x = f(a), y = f(b);
if(x != y) {
V[a][b] = V[b][a] = c;
r[x] = y;
ans += c;
cnt++;
continue;
}

int dist = shortestPath(a, b);
if(c < dist) {
V[a][b] = V[b][a] = c;
ans += c;
}
if(c > dist) {
bw.write("-1");
return;
}
}

if(cnt < N-1) bw.write("-1");
else bw.write(ans + "\n");

}

static int shortestPath(int start, int end) {
int[][] D = new int[N][N];
for(int i=0;i<N;i++) for(int j=0;j<N;j++) {
if(V[i][j] != 0) D[i][j] = V[i][j];
else D[i][j] = INF;
}

for(int i=0;i<N;i++) for(int j=0;j<N;j++) for(int k=0;k<N;k++) if(D[j][k] > D[j][i] + D[i][k]) D[j][k] = D[j][i] + D[i][k];

return D[start][end];
}

}

```
75 changes: 75 additions & 0 deletions 권혁준_13주차/[BOJ-1561] 놀이 공원.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
```java

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

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 long N;
static int M;
static int[] A;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

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

}

static void solve() throws Exception{

long s = 0, e = (long)1e15, m = (s+e)>>1;
while(s<e) {
long cnt = M;
for(int i=0;i<M;i++) cnt += m/A[i];
if(cnt < N) s = m+1;
else e = m;
m = (s+e)>>1;
}

if(m == 0) {
bw.write(N + "\n");
return;
}
N -= M;
for(int i=0;i<M;i++) N -= (m-1)/A[i];
for(int i=0;i<M;i++) if(m % A[i] == 0) {
if(--N == 0) {
bw.write((i+1) + "\n");
return;
}
}

}

}

```
111 changes: 111 additions & 0 deletions 권혁준_13주차/[BOJ-16118] 달빛 여우.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
```java
import java.util.*;
import java.io.*;

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 List<int[]>[] V;
static final int INF = (int)2e9;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = 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*2});
V[b].add(new int[]{a,c*2});
}

}

static void solve() throws Exception{

int[] DA = new int[N+1];
Arrays.fill(DA, INF);
DA[1] = 0;
PriorityQueue<int[]> Q = new PriorityQueue<>((a,b) -> a[0]-b[0]);
Q.offer(new int[]{0,1});

while(!Q.isEmpty()){
int[] now = Q.poll();
int d = now[0], n = now[1];
if(d > DA[n]) continue;
for(int[] next:V[n]){
int i = next[0], c = next[1];
if(DA[i] > d+c){
DA[i] = d+c;
Q.offer(new int[]{DA[i],i});
}
}
}

int[][] DB = new int[N+1][2];
for(int i=1;i<=N;i++) Arrays.fill(DB[i], INF);
DB[1][0] = 0;
Q = new PriorityQueue<>((a,b) -> a[0]/2 + a[1]*2 - b[0]/2 - b[1]*2);
Q.offer(new int[]{0,0,1,0});
while(!Q.isEmpty()){
int[] now = Q.poll();
int a = now[0], b = now[1], n = now[2], k = now[3];
if(a/2 + b*2 > DB[n][k]) continue;
for(int[] next:V[n]){
int i = next[0], c = next[1];
int res = a/2 + b*2;
if(k == 0){
res += c/2;
if(DB[i][k^1] > res){
DB[i][k^1] = res;
Q.offer(new int[]{a+c,b,i,k^1});
}
}
else{
res += c*2;
if(DB[i][k^1] > res){
DB[i][k^1] = res;
Q.offer(new int[]{a,b+c,i,k^1});
}
}
}
}

int ans = 0;
for(int i=2;i<=N;i++){
int a = DA[i], b = Math.min(DB[i][0], DB[i][1]);
if(a < b) ans++;
}
bw.write(ans + "\n");

}

}
```
Loading