Skip to content
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
139 changes: 139 additions & 0 deletions 권혁준_21주차/[BOJ-1033] 칵테일.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
```java
import java.math.BigInteger;
import java.util.*;
import java.io.*;

class IOController {
BufferedReader br;
BufferedWriter bw;
StringTokenizer st;

public IOController() {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer("");
}

String nextLine() throws Exception {
String line = br.readLine();
st = new StringTokenizer(line);
return line;
}

String nextToken() throws Exception {
while (!st.hasMoreTokens()) nextLine();
return st.nextToken();
}

int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}

void close() throws Exception {
bw.flush();
bw.close();
}

void write(String content) throws Exception {
bw.write(content);
}

}

public class Main {

static IOController io;

//

static int N;
static long[] A;
static List<int[]>[] V;

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

io = new IOController();

init();
solve();

io.close();

}

public static void init() throws Exception {

N = io.nextInt();
V = new List[N];
for(int i=0;i<N;i++) V[i] = new ArrayList<>();
for(int i=1;i<N;i++) {
int a = io.nextInt(), b = io.nextInt(), c = io.nextInt(), d = io.nextInt();
long g = gcd(c,d);
c /= g;
d /= g;
V[a].add(new int[]{b, c, d});
V[b].add(new int[]{a, d, c});
}
A = new long[N];
Arrays.fill(A, 1);

}

static void solve() throws Exception {

dfs(0,-1);
for(int i=0;i<N;i++) io.write(A[i] + " ");

}

static void dfs(int n, int p) {
for(int[] e:V[n]) if(e[0] != p) {
int i = e[0], a = e[1], b = e[2];
dfs(i,n);
A[n] *= A[i] * a;
}
change(n,p);
long g = subgcd(n, p);
div(n,p,g);
}

static long gcd(long a, long b) {
if(a < b) {
long temp = a;
a = b;
b = temp;
}
return a%b == 0 ? b : gcd(b, a%b);
}

static long subgcd(int n, int p) {
long g = A[n];
for(int[] e:V[n]) if(e[0] != p) {
g = gcd(subgcd(e[0], n), g);
}
return g;
}

static void div(int n, int p, long g) {
A[n] /= g;
for(int[] e:V[n]) if(e[0] != p) div(e[0],n,g);
}

static void change(int n, int p) {
for(int[] e:V[n]) if(e[0] != p) {
int i = e[0], a = e[1], b = e[2];
A[i] = A[n]*b/a;
change(e[0],n);
}
}

}
```
143 changes: 143 additions & 0 deletions 권혁준_21주차/[BOJ-22866] 탑 보기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
```java
import java.math.BigInteger;
import java.util.*;
import java.io.*;

class IOController {
BufferedReader br;
BufferedWriter bw;
StringTokenizer st;

public IOController() {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer("");
}

String nextLine() throws Exception {
String line = br.readLine();
st = new StringTokenizer(line);
return line;
}

String nextToken() throws Exception {
while (!st.hasMoreTokens()) nextLine();
return st.nextToken();
}

int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}

void close() throws Exception {
bw.flush();
bw.close();
}

void write(String content) throws Exception {
bw.write(content);
}

}

public class Main {

static IOController io;

//

static int N;
static List<int[]> A;

static List<Integer>[] V;
static int[] C, deg, ans, dist;

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

io = new IOController();

init();
solve();

io.close();

}

public static void init() throws Exception {

N = io.nextInt();
A = new ArrayList<>();
for(int i=0;i<N;i++) A.add(new int[]{io.nextInt(), i});
C = new int[N];
ans = new int[N];
Arrays.fill(ans, -1);
dist = new int[N];

}

static void solve() throws Exception {

process();
Collections.reverse(A);
process();
for(int i=0;i<N;i++) {
io.write(C[i] + " ");
if(ans[i] != -1) io.write(ans[i] + " ");
io.write("\n");
}

}

static void process() {
constructTree(A);
for(int i=0;i<N;i++) if(deg[i] == 0) dfs(i,-1,0);
}

static void constructTree(List<int[]> list) {

deg = new int[N];
V = new List[N];
for(int i=0;i<N;i++) V[i] = new ArrayList<>();

int n = list.size();
Stack<int[]> stack = new Stack<>();
for(int i=0;i<n;i++) {
int h = list.get(i)[0], x = list.get(i)[1];
while(!stack.isEmpty() && h > stack.peek()[0]) {
int index = stack.pop()[1];
V[x].add(index);
deg[index]++;
}
stack.push(new int[]{h, x});
}

}

static void dfs(int n, int p, int d) {
C[n] += d;
if(p != -1) {
if(ans[n] == -1) {
ans[n] = p+1;
dist[n] = Math.abs(p-n);
}
else {
if(dist[n] >= Math.abs(p-n)) {
ans[n] = p+1;
dist[n] = Math.abs(p-n);
}
}
}

for(int i:V[n]) dfs(i,n,d+1);
}

}
```
113 changes: 113 additions & 0 deletions 권혁준_21주차/[BOJ-24337] 가희와 탑.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
```java
import java.math.BigInteger;
import java.util.*;
import java.io.*;

class IOController {
BufferedReader br;
BufferedWriter bw;
StringTokenizer st;

public IOController() {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer("");
}

String nextLine() throws Exception {
String line = br.readLine();
st = new StringTokenizer(line);
return line;
}

String nextToken() throws Exception {
while (!st.hasMoreTokens()) nextLine();
return st.nextToken();
}

int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}

void close() throws Exception {
bw.flush();
bw.close();
}

void write(String content) throws Exception {
bw.write(content);
}

}

public class Main {

static IOController io;

//

static int N, A, B;

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

io = new IOController();

init();
solve();

io.close();

}

public static void init() throws Exception {

N = io.nextInt();
A = io.nextInt();
B = io.nextInt();

}

static void solve() throws Exception {

if(A + B > N+1) {
io.write("-1\n");
return;
}
int[] ans = new int[N];
if(A == 1) {
ans[0] = B;
int temp = 1;
for(int i=N-1,k=1;k++<B;i--) ans[i] = temp++;
for(int i=0;i<N;i++) io.write((ans[i] == 0 ? 1 : ans[i]) + " ");
return;
}
if(B == 1) {
int temp = 1;
for(int i=N-A;i<N;i++) ans[i] = temp++;
for(int i=0;i<N;i++) io.write((ans[i] == 0 ? 1 : ans[i]) + " ");
return;
}


int mid = N-B;
ans[mid] = Math.max(A,B);
int temp = 1;
for(int i=N-1;i>mid;i--) ans[i] = temp++;
temp = 1;
for(int i=mid-A+1;i<mid;i++) ans[i] = temp++;
for(int i=0;i<mid-A+1;i++) ans[i] = 1;

for(int i=0;i<N;i++) io.write(ans[i] + " ");

}

}
```
Loading