Skip to content

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

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 8 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
73 changes: 73 additions & 0 deletions 권혁준_11주차/[BOJ-13397] 구간 나누기 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
```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;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception {

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

}

static void solve() throws Exception {

for(int k=0;k<=10000;k++) {
int min = A[0], max = A[0], cnt = 1;
for(int i=1;i<N;i++) {
int tmin = Math.min(min, A[i]);
int tmax = Math.max(max, A[i]);
if(tmax-tmin > k) {
cnt++;
min = A[i];
max = A[i];
}
else {
min = tmin;
max = tmax;
}
}
if(cnt <= M) {
bw.write(k + "\n");
return;
}
}

}

}

```
50 changes: 50 additions & 0 deletions 권혁준_11주차/[BOJ-1451] 직사각형으로 나누기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
```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



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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{



}

static void solve() throws Exception{



}

}

```
81 changes: 81 additions & 0 deletions 권혁준_11주차/[BOJ-1509] 팰린드롬 분할.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
```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 char[] A;
static boolean[][] isPalindrome;
static int[] dp;
static final int INF = 123456;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

A = br.readLine().toCharArray();
N = A.length;
isPalindrome = new boolean[N][N];

}

static void solve() throws Exception{

for(int i=0;i<N;i++) {
// i가 팰린드롬의 중심일 때

for(int k=0;i-k>=0 && i+k<N;k++) {
if(A[i-k] != A[i+k]) break;
isPalindrome[i-k][i+k] = true;
}

if(i < N-1 && A[i] == A[i+1]) {
// i, i+1이 팰린드롬의 중심일 때
for(int k=0;i-k>=0 && i+1+k<N;k++) {
if(A[i-k] != A[i+1+k]) break;
isPalindrome[i-k][i+k+1] = true;
}
}
}

dp = new int[N];
Arrays.fill(dp, INF);
dp[0] = 1;

for(int i=1;i<N;i++) {
if(isPalindrome[0][i]) dp[i] = 1;
for(int j=1;j<=i;j++) if(isPalindrome[j][i]) dp[i] = Math.min(dp[i], dp[j-1]+1);
}
bw.write(dp[N-1] + "\n");

}

}

```
111 changes: 111 additions & 0 deletions 권혁준_11주차/[BOJ-1944] 복제 로봇.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 int[][] A;
static int[][] info;
static List<int[]> E;
static int idx = 1;
static int[] r;

static int[] dx = {1,0,-1,0};
static int[] dy = {0,1,0,-1};

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();
M = nextInt();
A = new int[N][N];
info = new int[M+1][];
for(int i=0;i<N;i++) {
char[] temp = br.readLine().toCharArray();
for(int j=0;j<N;j++) {
if(temp[j] == '1') A[i][j] = -2;
if(temp[j] == '0') A[i][j] = -1;
if(temp[j] == 'S') {
info[0] = new int[] {i,j};
A[i][j] = 0;
}
if(temp[j] == 'K') {
info[idx] = new int[] {i,j};
A[i][j] = idx++;
}
}
}
E = new ArrayList<>();
r = new int[M+1];
for(int i=0;i<=M;i++) r[i] = i;

}

static void solve() throws Exception{

for(int i=0;i<=M;i++) {
Queue<int[]> Q = new LinkedList<>();
boolean[][] vis = new boolean[N][N];
vis[info[i][0]][info[i][1]] = true;
Q.offer(new int[] {info[i][0], info[i][1], 0});
while(!Q.isEmpty()) {
int[] now = Q.poll();
int x = now[0], y = now[1], t = now[2];
if(A[x][y] >= 0 && A[x][y] != i) E.add(new int[] {i,A[x][y],t});
for(int k=0;k<4;k++) {
int xx = x+dx[k], yy = y+dy[k];
if(xx<0 || xx>=N || yy<0 || yy>=N || vis[xx][yy] || A[xx][yy] == -2) continue;
vis[xx][yy] = true;
Q.offer(new int[] {xx,yy,t+1});
}
}
}

Collections.sort(E, (a,b) -> a[2]-b[2]);

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

bw.write((cnt == M ? mst : -1) + "\n");

}

}

```
Loading