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

[27주차] 백제완 #356

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
KodaHye merged 5 commits into GreatAlgorithm-Study:main from jewan100:main
Mar 30, 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
36 changes: 36 additions & 0 deletions BOJ/1000-5000번/JW_1644.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.ArrayList;

public class JW_1644 {

public static void main(String[] args) throws Exception {
int n = read();
int cnt = 0;
boolean[] isPrime = new boolean[n + 1];
isPrime[0] = isPrime[1] = true;
ArrayList<Integer> prime = new ArrayList<>();
for (int i = 2; i < n + 1; i++) {
if (isPrime[i])
continue;
prime.add(i);
for (int j = i * 2; j < n + 1; j += i)
isPrime[j] = true;
}
int l = 0, r = 0, sum = 0;
while (r < prime.size()) {
sum += prime.get(r);
while (sum > n && l <= r)
sum -= prime.get(l++);
if (sum == n)
cnt++;
r++;
}
System.out.println(cnt);
}

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);
return n;
}
}
31 changes: 31 additions & 0 deletions BOJ/10001-15000번/JW_14852.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Arrays;
import java.util.PriorityQueue;

public class JW_14852 {
static long dp[];
static final int MOD = 1_000_000_007;

public static void main(String[] args) throws Exception {
int n = read();
dp = new long[n + 1];
dp[0] = 1;
dp[1] = 2;
if (n > 1)
dp[2] = 7;
long temp = 20;
for (int i = 3; i <= n; i++) {
dp[i] = (temp + dp[i - 2]) % MOD;
temp = (temp + dp[i] * 2) % MOD;
}
System.out.println(dp[n]);
}

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;
}
}
56 changes: 56 additions & 0 deletions BOJ/15001-20000번/JW_18513.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;

public class JW_18513 {

public static void main(String[] args) throws Exception {
int n = read(), k = read();
Deque<int[]> dq = new ArrayDeque<>();
HashSet<Integer> hs = new HashSet<>();
for (int i = 0; i < n; i++) {
int p = read();
dq.offer(new int[] { p, p });
hs.add(p);
}
long answer = 0;
while (!dq.isEmpty()) {
int size = dq.size();
while (size-- > 0) {
int[] cur = dq.poll();
int next = cur[0] + 1;
if (!hs.contains(next)) {
dq.offer(new int[] { next, cur[1] });
hs.add(next);
answer += Math.abs(next - cur[1]);
if (--k == 0) {
System.out.println(answer);
return;
}
}
next = cur[0] - 1;
if (!hs.contains(next)) {
dq.offer(new int[] { next, cur[1] });
hs.add(next);
answer += Math.abs(next - cur[1]);
if (--k == 0) {
System.out.println(answer);
return;
}
}
}
}
}

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;
}
}
136 changes: 136 additions & 0 deletions CodeTree/2021-2022년/JW_나무박멸.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class JW_나무박멸 {

static int n, m, k, c;
static int[][] board, visited;
static int[] dy = { -1, 1, 0, 0, -1, -1, 1, 1 }, dx = { 0, 0, -1, 1, -1, 1, 1, -1 };

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
board = new int[n][n];
visited = new int[n][n];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++)
board[i][j] = Integer.parseInt(st.nextToken());
}
int total = 0;
while (m-- > 0) {
grow();
breed();
int[] target = find();
total += target[2];
if (target[0] == -1 && target[1] == -1)
break;
kill(target);
heal();
}
System.out.println(total);
}

private static void heal() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (visited[i][j] > 0)
visited[i][j]--;
}

private static void grow() {
int[][] dBoard = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (board[i][j] > 0) {
for (int d = 0; d < 4; d++) {
int ny = i + dy[d], nx = j + dx[d];
if (isValid(ny, nx) && board[ny][nx] > 0)
dBoard[i][j]++;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
board[i][j] += dBoard[i][j];
}

private static void breed() {
Deque<int[]> dq = new ArrayDeque<>();
int[][] dBoard = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (board[i][j] > 0) {
for (int d = 0; d < 4; d++) {
int ny = i + dy[d], nx = j + dx[d];
if (isValid(ny, nx) && visited[ny][nx] == 0 && board[ny][nx] == 0)
dq.offer(new int[] { ny, nx });
}
if (dq.isEmpty())
continue;
int amount = board[i][j] / dq.size();
while (!dq.isEmpty()) {
int[] pos = dq.poll();
dBoard[pos[0]][pos[1]] += amount;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
board[i][j] += dBoard[i][j];
}

private static int[] find() {
PriorityQueue<int[]> pq = new PriorityQueue<>(
(o1, o2) -> o1[2] != o2[2] ? o2[2] - o1[2] : o1[0] != o2[0] ? o1[0] - o2[0] : o1[1] - o2[1]);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (board[i][j] > 0) {
int sum = board[i][j];
for (int d = 4; d < 8; d++) {
for (int l = 1; l <= k; l++) {
int ny = i + dy[d] * l, nx = j + dx[d] * l;
if (isValid(ny, nx) && board[ny][nx] > 0)
sum += board[ny][nx];
else
break;
}
}
pq.offer(new int[] { i, j, sum });
}
if (pq.isEmpty())
return new int[] { -1, -1, 0 };
return pq.poll();
}

private static void kill(int[] target) {
int y = target[0], x = target[1];
board[y][x] = 0;
visited[y][x] = c + 1;
for (int d = 4; d < 8; d++) {
for (int l = 1; l <= k; l++) {
int ny = y + dy[d] * l, nx = x + dx[d] * l;
if (isValid(ny, nx)) {
visited[ny][nx] = c + 1;
if (board[ny][nx] <= 0) {
break;
} else {
board[ny][nx] = 0;
}
}
}
}
}

private static boolean isValid(int y, int x) {
return 0 <= y && y < n && 0 <= x && x < n;
}
}
37 changes: 37 additions & 0 deletions Programmers/Level3/JW_72413.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Arrays;

class JW_72413 {
public int solution(int n, int s, int a, int b, int[][] fares) {
int INF = 100_001 * n;
int answer = INF;
int[][] graph = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
Arrays.fill(graph[i], INF);
graph[i][i] = 0;
}
for (int[] fare : fares) {
graph[fare[0]][fare[1]] = fare[2];
graph[fare[1]][fare[0]] = fare[2];
}
floydWarshall(graph, n);
for(int i = 1; i <= n; i++) {
int total = graph[s][i] + graph[i][a] + graph[i][b];
if (total < answer) {
answer = total;
}
}
return answer;
}

public void floydWarshall(int[][] graph, int n) {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (graph[i][k] + graph[k][j] < graph[i][j]) {
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}
}
}

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