Skip to content

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

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 5 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
130 changes: 130 additions & 0 deletions 권혁준_19주차/[BOJ-14867] 물통.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
```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 A, B, EA, EB;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

A = nextInt();
B = nextInt();
EA = nextInt();
EB = nextInt();

}

static void solve() throws Exception {

boolean[] emptyA = new boolean[B+1];
boolean[] emptyB = new boolean[A+1];
boolean[] fullA = new boolean[B+1];
boolean[] fullB = new boolean[A+1];
Queue<int[]> Q = new LinkedList<>();

emptyA[0] = true;
emptyB[0] = true;
Q.offer(new int[]{0,0,0});
while(!Q.isEmpty()){
int[] now = Q.poll();
int a = now[0], b = now[1], t = now[2];
if(a == EA && b == EB){
bw.write(t + "\n");
return;
}

if(a != A && !fullA[b] && (b != B || !fullB[A]) && (b != 0 || !emptyB[A])){
fullA[b] = true;
if(b == B) fullB[A] = true;
if(b == 0) emptyB[A] = true;
Q.offer(new int[]{A,b,t+1});
}
if(b != B && !fullB[a] && (a != A || !fullA[B]) && (a != 0 || !emptyA[B])){
fullB[a] = true;
if(a == A) fullA[B] = true;
if(a == 0) emptyA[B] = true;
Q.offer(new int[]{a,B,t+1});
}
if(a != 0 && !emptyA[b] && (b != B || !fullB[0]) && (b != 0 || !emptyB[0])){
emptyA[b] = true;
if(b == B) fullB[0] = true;
if(b == 0) emptyB[0] = true;
Q.offer(new int[]{0,b,t+1});
}
if(b != 0 && !emptyB[a] && (a != A || !fullA[0]) && (a != 0 || !emptyA[0])){
emptyB[a] = true;
if(a == A) fullA[0] = true;
if(a == 0) emptyA[0] = true;
Q.offer(new int[]{a,0,t+1});
}

if(a != 0) {
int diff = Math.min(a, B-b);
int na = a-diff, nb = b+diff;
if(na == 0){
if(!emptyA[nb] && (nb != B || !fullB[0])){
emptyA[nb] = true;
if(nb == B) fullB[0] = true;
Q.offer(new int[]{na,nb,t+1});
}
}
else{
if(!fullB[na]){
fullB[na] = true;
Q.offer(new int[]{na,nb,t+1});
}
}
}
if(b != 0) {
int diff = Math.min(b, A-a);
int na = a+diff, nb = b-diff;
if(nb == 0){
if(!emptyB[na] && (na != A || !fullA[0])){
emptyB[na] = true;
if(na == A) fullA[0] = true;
Q.offer(new int[]{na,nb,t+1});
}
}
else{
if(!fullA[nb]){
fullA[nb] = true;
Q.offer(new int[]{na,nb,t+1});
}
}
}
}
bw.write("-1");

}



}
```
86 changes: 86 additions & 0 deletions 권혁준_19주차/[BOJ-1508] 레이스.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
```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, K;
static int[] A;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

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

}

static void solve() throws Exception {

int s = 0, e = 1000000, m = (s+e+1)>>1;
String answer = "";
while(s<e){
String result = check(m);
if(result == null) e = m-1;
else {
answer = result;
s = m;
}
m = (s+e+1)>>1;
}
bw.write(answer);

}

static String check(int dist) {

for(int st=0;st<=K-M;st++){
String res = "1";
int last = A[st], cnt = 1;
for(int i=st+1;i<K;i++){
if(A[i] - last < dist) res += '0';
else {
if(cnt < M){
res += '1';
last = A[i];
cnt++;
}
else res += '0';
}
}
if(cnt == M) return res;
}
return null;

}
Comment on lines +63 to +83
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제를 풀다가 고민한 부분인데, 여태까지 모든 매개변수 탐색 문제를 풀 때 첫 번째 조건을 박아 놓고 시작했어. 무슨 말이냐면 이 문제 같은 경우 A[0]에 심판이 있다고 가정했단 말인데, 왜 이렇게 그리디하게 생각할 수 있는 거지? 첫 번째 위치에 심판이 없을 수도 있는거 아닌가?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이게 첫 시작 지점을 마음대로 정할 수 있어서 가능한 거라고 생각했어
A[0]에 심판이 없는 모든 경우는 A[0]에 심판이 있게 함으로써 항상 더 좋은 경우로 이끌어낼 수 있다고 생각했어


}
```
64 changes: 64 additions & 0 deletions 권혁준_19주차/[BOJ-2696] 중앙값 구하기.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;
static PriorityQueue<Integer> low, high;

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

int T = nextInt();
for(int i=1;i<=T;solve(i++)) ready();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
low = new PriorityQueue<>((a,b) -> b-a);
high = new PriorityQueue<>((a,b) -> a-b);

}

static void solve(int tc) throws Exception {

bw.write((N+1)/2 + "\n");
for(int i=1;i<=N;i++){
int a = nextInt();
if(i%2 == 1) low.add(a);
else high.add(a);
if(!high.isEmpty() && low.peek() > high.peek()){
int l = low.poll(), h = high.poll();
low.add(h);
high.add(l);
}
if(i%2 == 1) bw.write(low.peek() + " ");
if(i%20 == 19) bw.write("\n");
}
bw.write("\n");

}

}
```
93 changes: 93 additions & 0 deletions 권혁준_19주차/[BOJ-3108] 로고.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
```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 int[] r;

static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}
static int comp(int x, int y) {return x*1001 + y;}

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
r = new int[1001*1001];
for(int i=0;i<1001*1001;i++) r[i] = i;

}

static void solve() throws Exception {

List<Integer> L = new ArrayList<>();
L.add(comp(500,500));
for(int i=0;i<N;i++){
int x1 = nextInt() + 500;
int y1 = nextInt() + 500;
int x2 = nextInt() + 500;
int y2 = nextInt() + 500;
for(int j=x1;j<x2;j++) {
{
int a = comp(j,y1), b = comp(j+1,y1);
int fa = f(a), fb = f(b);
if(fa != fb) r[fa] = fb;
}
{
int a = comp(j,y2), b = comp(j+1,y2);
int fa = f(a), fb = f(b);
if(fa != fb) r[fa] = fb;
}
L.add(comp(j,y1));
L.add(comp(j+1,y2));
}
for(int j=y1;j<y2;j++) {
{
int a = comp(x1,j), b = comp(x1,j+1);
int fa = f(a), fb = f(b);
if(fa != fb) r[fa] = fb;
}
{
int a = comp(x2,j), b = comp(x2,j+1);
int fa = f(a), fb = f(b);
if(fa != fb) r[fa] = fb;
}
L.add(comp(x1,j+1));
L.add(comp(x2,j));
}
}

Set<Integer> S = new TreeSet<>();
for(int i:L) S.add(f(i));
bw.write((S.size()-1) + "\n");

}

}
```
Loading