Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[23주차] 백제완 #314

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

Merged
jewan100 merged 6 commits into GreatAlgorithm-Study:main from jewan100:main
Feb 23, 2025
Merged
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
53 changes: 53 additions & 0 deletions BOJ/1000-5000번/JW_1135.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.ArrayList;
import java.util.Collections;

public class JW_1135 {

static int n;
static int[] dp;
static ArrayList<ArrayList<Integer>> tree = new ArrayList<>();

public static void main(String[] args) throws Exception {
n = read();
dp = new int[n];
// 트리 구조 만들기
for (int i = 0; i < n; i++) {
tree.add(new ArrayList<>());
int parent = read();
// 루트 제외
if (parent == -1)
continue;
tree.get(parent).add(i);
}
recursive(0);
System.out.println(dp[0]);
}

// 재귀적으로 해당 노드가 걸리는 계산
private static void recursive(int cur) {
ArrayList<Integer> al = new ArrayList<>();
for (int next : tree.get(cur)) {
recursive(next);
al.add(dp[next]); // 탐색한 노드가 가지는 값을 저장
}
// 그리디하게, 가장 오래 걸리는 노드를 먼저 탐색해줘야 함
al.sort(Collections.reverseOrder());

// 1초에 한 번씩 전파 가능
// 최댓값이 결국 최솟값이 됨
for (int i = 0; i < al.size(); i++)
dp[cur] = Math.max(dp[cur], al.get(i) + i + 1);
}

private static int read() throws Exception {
int c, n = System.in.read() & 15;
boolean m = n == 13;
if (m)
n = System.in.read() & 15;
while ((c = System.in.read()) >= 48)
n = (n << 3) + (n << 1) + (c & 15);
if (c == 13)
System.in.read();
return m ? ~n + 1 : n;
}
}
73 changes: 73 additions & 0 deletions BOJ/1000-5000번/JW_1450.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.Map;
import java.util.TreeMap;

public class JW_1450 {

static int n, c;
static TreeMap<Long, Integer> tmA = new TreeMap<>();
static TreeMap<Long, Integer> tmB = new TreeMap<>();
static TreeMap<Long, Integer> prefixSumA = new TreeMap<>();
static TreeMap<Long, Integer> prefixSumB = new TreeMap<>();

public static void main(String[] args) throws Exception {
n = read();
c = read();
int[] aArr = new int[n / 2];
int[] bArr = new int[n - n / 2];

for (int i = 0; i < aArr.length; i++)
aArr[i] = read();
for (int i = 0; i < bArr.length; i++)
bArr[i] = read();
recursive(0, 0, aArr, tmA);
recursive(0, 0, bArr, tmB);

makePrefixSum(tmA, prefixSumA);
makePrefixSum(tmB, prefixSumB);

System.out.println(calculate());
}

private static void recursive(int depth, long sum, int[] arr, TreeMap<Long, Integer> map) {
if (sum > c)
return;
if (depth == arr.length) {
map.put(sum, map.getOrDefault(sum, 0) + 1);
return;
}
recursive(depth + 1, sum + arr[depth], arr, map);
recursive(depth + 1, sum, arr, map);
}

private static void makePrefixSum(TreeMap<Long, Integer> tm, TreeMap<Long, Integer> prefixSum) {
int sum = 0;
for (Map.Entry<Long, Integer> entry : tm.entrySet()) {
sum += entry.getValue();
prefixSum.put(entry.getKey(), sum);
}
}

private static long calculate() {
long count = 0;

for (Map.Entry<Long, Integer> entry : tmA.entrySet()) {
long aSum = entry.getKey();
int aWays = entry.getValue();
Map.Entry<Long, Integer> bEntry = prefixSumB.floorEntry(c - aSum);
Copy link
Contributor

Choose a reason for hiding this comment

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

TreeMap을 이용하는 방법도 있군요. 그리고 floorEntry()를 이용해서 개수를 구할 생각은 못해봤는데 이것도 좋은 아이디어인 것 같습니다

if (bEntry != null) {
count += (long) aWays * bEntry.getValue();
}
}

return count;
}

private static int read() throws Exception {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) >= 48)
n = (n << 3) + (n << 1) + (c & 15);
if (c == 13)
System.in.read();
return n;
}
}
160 changes: 160 additions & 0 deletions BOJ/15001-20000번/JW_19237.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import java.util.ArrayDeque;
import java.util.Deque;

public class JW_19237 {

// 상어 객체
static class Shark {
int num, idx, dir; // 상어의 번호, 인덱스, 방향
int[][] priority; // 해당 상어의 위치에 따른 방향 우선 순위

Shark(int num, int idx, int dir, int[][] priority) {
this.num = num;
this.idx = idx;
this.dir = dir;
this.priority = priority;
}

// 방향 우선 순위 반환
int[] getPriority() {
return priority[dir];
}
}

static int n, m, k;
static int[] scentBoard, remainBoard, move; // 냄새, 남은 시간, 방향 벡터
static boolean[] visited;
static Deque<Shark> sharks = new ArrayDeque<>(); // 상어 큐
static Deque<int[]> scentDeque = new ArrayDeque<>(); // 냄새 큐

public static void main(String[] args) throws Exception {
n = read();
m = read();
k = read();
move = new int[] { 0, -n, n, -1, 1 };
scentBoard = new int[n * n];
remainBoard = new int[n * n];
visited = new boolean[n * n];

// 기본 정보 입력
init();

int time = 0;
while (time <= 1000) {
int size = sharks.size();

// 1번 상어만 남았다면 종료
if (size == 1) {
System.out.println(time);
return;
}

visited = new boolean[n * n]; // 방문 배열 초기화
// 상어를 작은 번호 순대로 꺼내서 탐색
while (size-- > 0) {
Shark shark = sharks.poll();
int num = shark.num, idx = shark.idx;
int nextDir = getNextDir(shark); // 다음 방향 결정
int next = idx + move[nextDir]; // 이동

// 이미 도착해있는 상어가 있다면
// 즉, 자신보다 작은 번호의 상어가 도착해있다면 쫓겨남
if (visited[next])
continue;

visited[next] = true;
shark.idx = next;
shark.dir = nextDir;
sharks.offer(shark); // 상어 큐에 삽입
scentDeque.offer(new int[] { next, num }); // 냄새 큐에 삽입
}
removeScent(); // 냄새 카운트 감소
putScent(); // 냄새 배열에 적용
time++;
}
System.out.println(-1);
}

// 기본 정보 입력
private static void init() throws Exception {
int[] tempSharks = new int[m + 1];
for (int i = 0; i < n * n; i++) {
int num = read();
if (num != 0) {
tempSharks[num] = i; // 상어의 인덱스 저장
scentBoard[i] = num; // 냄새 삽입
remainBoard[i] = k; // 남은 시간 기록
}
}
int[] tempDir = new int[m + 1];
for (int i = 1; i < m + 1; i++)
tempDir[i] = read();

// 상어 객체 생성 및 큐에 삽입
for (int i = 1; i < m + 1; i++) {
int idx = tempSharks[i], dir = tempDir[i];
int[][] priority = new int[5][5]; // 우선 순위 입력
for (int j = 1; j < 5; j++)
for (int k = 1; k < 5; k++)
priority[j][k] = read();
Shark shark = new Shark(i, idx, dir, priority);
sharks.offer(shark);
}
}

// 다음 방향 결정
private static int getNextDir(Shark shark) {
int[] priority = shark.getPriority(); // 우선 순위에 따라 방향 결정
int myScent = 0;
for (int i = 1; i < 5; i++) {
int dir = priority[i];
int next = shark.idx + move[dir];
if (isValid(shark.idx, next)) {
if (scentBoard[next] == 0)
return dir;
else if (scentBoard[next] == shark.num && myScent == 0)
myScent = dir;
}
}
return myScent;
}

// 냄새 카운트 감소
private static void removeScent() {
for (int i = 0; i < n * n; i++)
if (scentBoard[i] != 0 && --remainBoard[i] == 0)
scentBoard[i] = 0;
}

// 냄새 큐에 있는 좌표에 냄새 삽입
private static void putScent() {
while (!scentDeque.isEmpty()) {
int[] scent = scentDeque.poll();
int idx = scent[0], num = scent[1];
scentBoard[idx] = num;
remainBoard[idx] = k;
}
}

// 인덱스 유효성 검사
private static boolean isValid(int cur, int next) {
// 전체 인덱스를 벗어나는 경우
if (next < 0 || next >= n * n)
return false;
// 좌, 우로 움직일 때, 같은 행에 있는지 확인
int curRow = cur / n;
int nextRow = next / n;
if (Math.abs(cur - next) == 1 && curRow != nextRow)
return false;
return true;
}

private static int read() throws Exception {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) >= 48)
n = (n << 3) + (n << 1) + (c & 15);
if (c == 13)
System.in.read();
return n;
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /