Skip to content

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

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 9 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 권혁준_14주차/[BOJ-1114] 통나무 자르기.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 int L, K, C;
static int[] A;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

L = nextInt();
K = nextInt();
C = nextInt();
A = new int[K];
for(int i=0;i<K;i++) A[i] = nextInt();

}

static void solve() throws Exception{

Arrays.sort(A);
int s = A[0], e = L, m = (s+e)>>1;
for(int i=1;i<K;i++) s = Math.max(s, A[i]-A[i-1]);
while(s<e) {
int cnt = 0;
int prev = 0, max = 0;

for(int i=0;i<K;i++) {
if(A[i] - prev > m) {
max = Math.max(max, A[i-1]-prev);
prev = A[i-1];
cnt++;
}
}
if(L - prev > m) {
max = Math.max(max, A[K-1]-prev);
prev = A[K-1];
cnt++;
}
max = Math.max(max, L-prev);
if(cnt > C || max > m) s = m+1;
else e = m;
m = (s+e)>>1;
}

int len = m;

// i에서 처음 자르기
for(int i=0;i<K;i++) {
int cnt = 1, prev = A[i], max = A[i];
for(int j=i+1;j<K;j++) {
if(A[j] - prev > len) {
max = Math.max(max, A[j-1]-prev);
prev = A[j-1];
cnt++;
}
}
if(L - prev > len) {
max = Math.max(max, A[K-1]-prev);
prev = A[K-1];
cnt++;
}
max = Math.max(max, L-prev);
if(cnt > C || max != len) continue;
bw.write(len + " " + A[i]);
return;
}
}

}
```
75 changes: 75 additions & 0 deletions 권혁준_14주차/[BOJ-12764] 싸지방에 간 준하.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;
static TreeSet<Integer> S;
static int[][] info;
static int[] num, cnt;
static int max = 0;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
info = new int[N*2][2];
for(int i=1;i<=N;i++) {
int p = nextInt(), q = nextInt();
info[(i-1)*2] = new int[] {p,i};
info[(i-1)*2+1] = new int[] {q,-i};
}
S = new TreeSet<>();
for(int i=1;i<=N;i++) S.add(i);
num = new int[N+1];
cnt = new int[N+1];

}

static void solve() throws Exception{

Arrays.sort(info, (a,b) -> a[0]-b[0]);
for(int[] e:info) {
int v = e[0], id = Math.abs(e[1]);
boolean out = e[1] < 0;

if(out) S.add(num[id]);
else {
num[id] = S.pollFirst();
max = Math.max(max, num[id]);
cnt[num[id]]++;
}
}
bw.write(max + "\n");
for(int i=1;i<=max;i++) bw.write(cnt[i] + " ");

}

}
```
100 changes: 100 additions & 0 deletions 권혁준_14주차/[BOJ-14391] 종이 조각.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
```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 char[][] A;
// 가로 : 1, 세로 : 2
static int[][] used;
static int ans = 0;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
A = new char[N][M];
for(int i=0;i<N;i++) A[i] = br.readLine().toCharArray();
used = new int[N][M];

}

static void solve() throws Exception{

bck(0,0,0);
bw.write(ans + "\n");

}

static void bck(int x, int y, int cur) {
ans = Math.max(ans, cur);
if(x == N) return;

while(used[x][y] != 0) {
y++;
if(y == M) {
y = 0;
x++;
}
if(x == N) return;
}

// (x,y)에서 시작하는 가로

if(y==0 || (y>0 && used[x][y-1] != 1)) {
String temp = "";
int k = y;
for(;k<M;k++) {
if(used[x][k] != 0) break;
temp += A[x][k];
used[x][k] = 1;
bck(x,y,cur + Integer.parseInt(temp));
}
for(int j=y;j<k;j++) used[x][j] = 0;
}

// (x,y)에서 시작하는 세로

if(x == 0 || (x>0 && used[x-1][y] != 2)) {
String temp = "";
int k = x;
for(;k<N;k++) {
if(used[k][y] != 0) break;
temp += A[k][y];
used[k][y] = 2;
bck(x,y,cur + Integer.parseInt(temp));
}
for(int i=x;i<k;i++) used[i][y] = 0;
}

}

}
```
101 changes: 101 additions & 0 deletions 권혁준_14주차/[BOJ-1445] 일요일 아침의 데이트.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
```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[] dx = {0,1,0,-1};
static final int[] dy = {1,0,-1,0};

static int N, M;
static char[][] A;
static int[][] P;
static int sx = -1, sy = -1;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
A = new char[N][];
P = new int[N][M];
for(int i=0;i<N;i++) {
A[i] = br.readLine().toCharArray();
for(int j=0;j<M;j++) {
if(A[i][j] == 'S') {
sx = i;
sy = j;
}
if(A[i][j] == 'g') {
for(int k=0;k<4;k++) {
int x = i+dx[k], y = j+dy[k];
if(0<=x && x<N && 0<=y && y<M) P[x][y] = 1;
}
}
}
}
for(int i=0;i<N;i++) for(int j=0;j<M;j++) if(A[i][j] == 'g') P[i][j] = 0;

}

static void solve() throws Exception{

PriorityQueue<int[]> Q = new PriorityQueue<>((a,b) -> a[0]==b[0] ? a[1]-b[1] : a[0]-b[0]);
int[][] D = new int[N][M];
int[][] C = new int[N][M];
for(int i=0;i<N;i++) {
Arrays.fill(D[i], 12345678);
Arrays.fill(C[i], 12345678);
}
Q.offer(new int[] {0,0,sx,sy});
D[sx][sy] = 0;
C[sx][sy] = 0;
while(!Q.isEmpty()) {
int[] now = Q.poll();
int d = now[0], c = now[1], x = now[2], y = now[3];
for(int i=0;i<4;i++) {
int xx = x+dx[i], yy = y+dy[i];
if(xx<0 || xx>=N || yy<0 || yy>=M) continue;
if(A[xx][yy] == 'F') {
bw.write(d + " "+ c);
return;
}
int dd = d + (A[xx][yy] == 'g' ? 1 : 0);
int cc = c + P[xx][yy];
if(D[xx][yy] > dd || (D[xx][yy] == dd && C[xx][yy] > cc)) {
D[xx][yy] = dd;
C[xx][yy] = cc;
Q.offer(new int[] {dd,cc,xx,yy});
}
}
}

}

}
```
Loading