-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 16-wkdghdwns199
- Loading branch information
Showing
15 changed files
with
343 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
# dohwaji[i][0] += 1000000000 | ||
# dohwaji[i][1] += 1000000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
n = int(input()) | ||
dohwaji = [tuple(map(int,(input().split()))) for _ in range(n)] | ||
|
||
# for i in range(len(dohwaji)): | ||
# dohwaji[i][0] += 1000000000 | ||
# dohwaji[i][1] += 1000000000 | ||
|
||
dohwaji.sort() | ||
|
||
line = dohwaji[0][1] | ||
answer = dohwaji[0][1] - dohwaji[0][0] | ||
|
||
for left, right in dohwaji[1:]: | ||
if right <= line: | ||
continue | ||
elif line < left: | ||
answer += right - left | ||
line = right | ||
elif line >= left and right > line: | ||
answer += right - line | ||
line = right | ||
# print(answer) | ||
|
||
print(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import sys | ||
|
||
input = sys.stdin.readline | ||
|
||
|
||
def func(): | ||
distances[1] = 0 | ||
|
||
for i in range(N): | ||
for j in range(M): | ||
cur_node, next_node, weight = edges[j] | ||
if distances[cur_node] != float('inf') and distances[next_node] > distances[cur_node] + weight: | ||
distances[next_node] = distances[cur_node] + weight | ||
if i == N-1: | ||
return False | ||
|
||
return True | ||
|
||
|
||
N, M = map(int, input().split()) | ||
|
||
edges = [] | ||
distances = [float('inf')] * (N+1) | ||
q = [] | ||
|
||
for _ in range(M): | ||
start, end, weight = map(int, input().split()) | ||
edges.append((start, end, weight)) | ||
|
||
|
||
if func(): | ||
for weight in distances[2:]: | ||
if weight == float('inf'): | ||
print(-1) | ||
else: | ||
print(weight) | ||
else: | ||
print(-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
''' | ||
๋ฌธ์ | ||
N๊ฐ์ ๋์๊ฐ ์๋ค. ๊ทธ๋ฆฌ๊ณ ํ ๋์์์ ์ถ๋ฐํ์ฌ ๋ค๋ฅธ ๋์์ ๋์ฐฉํ๋ ๋ฒ์ค๊ฐ M๊ฐ ์๋ค. | ||
๊ฐ ๋ฒ์ค๋ A, B, C๋ก ๋ํ๋ผ ์ ์๋๋ฐ, A๋ ์์๋์, B๋ ๋์ฐฉ๋์, C๋ ๋ฒ์ค๋ฅผ ํ๊ณ ์ด๋ํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด๋ค. | ||
์๊ฐ C๊ฐ ์์๊ฐ ์๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. C = 0์ธ ๊ฒฝ์ฐ๋ ์๊ฐ ์ด๋์ ํ๋ ๊ฒฝ์ฐ, C < 0์ธ ๊ฒฝ์ฐ๋ ํ์๋จธ์ ์ผ๋ก ์๊ฐ์ ๋๋์๊ฐ๋ ๊ฒฝ์ฐ์ด๋ค. | ||
1๋ฒ ๋์์์ ์ถ๋ฐํด์ ๋๋จธ์ง ๋์๋ก ๊ฐ๋ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. | ||
์ ๋ ฅ | ||
์ฒซ์งธ ์ค์ ๋์์ ๊ฐ์ N (1 โค N โค 500), ๋ฒ์ค ๋ ธ์ ์ ๊ฐ์ M (1 โค M โค 6,000)์ด ์ฃผ์ด์ง๋ค. | ||
๋์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์๋ ๋ฒ์ค ๋ ธ์ ์ ์ ๋ณด A, B, C (1 โค A, B โค N, -10,000 โค C โค 10,000)๊ฐ ์ฃผ์ด์ง๋ค. | ||
์ถ๋ ฅ | ||
๋ง์ฝ 1๋ฒ ๋์์์ ์ถ๋ฐํด ์ด๋ค ๋์๋ก ๊ฐ๋ ๊ณผ์ ์์ ์๊ฐ์ ๋ฌดํํ ์ค๋ ์ ์ผ๋ก ๋๋๋ฆด ์ ์๋ค๋ฉด ์ฒซ์งธ ์ค์ -1์ ์ถ๋ ฅํ๋ค. | ||
๊ทธ๋ ์ง ์๋ค๋ฉด N-1๊ฐ ์ค์ ๊ฑธ์ณ ๊ฐ ์ค์ 1๋ฒ ๋์์์ ์ถ๋ฐํด 2๋ฒ ๋์, 3๋ฒ ๋์, ..., N๋ฒ ๋์๋ก ๊ฐ๋ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ์ ์์๋๋ก ์ถ๋ ฅํ๋ค. | ||
๋ง์ฝ ํด๋น ๋์๋ก ๊ฐ๋ ๊ฒฝ๋ก๊ฐ ์๋ค๋ฉด ๋์ -1์ ์ถ๋ ฅํ๋ค. | ||
์์ ์ ๋ ฅ 1 | ||
3 4 | ||
1 2 4 | ||
1 3 3 | ||
2 3 -1 | ||
3 1 -2 | ||
์์ ์ถ๋ ฅ 1 | ||
4 | ||
3 | ||
์์ ์ ๋ ฅ 2 | ||
3 4 | ||
1 2 4 | ||
1 3 3 | ||
2 3 -4 | ||
3 1 -2 | ||
์์ ์ถ๋ ฅ 2 | ||
-1 | ||
์์ ์ ๋ ฅ 3 | ||
3 2 | ||
1 2 4 | ||
1 2 3 | ||
์์ ์ถ๋ ฅ 3 | ||
3 | ||
-1 | ||
''' | ||
|
||
import sys | ||
input = sys.stdin.readline | ||
INF = int(1e9) | ||
|
||
def bellman_ford(start): | ||
dist[start] = 0 | ||
|
||
for i in range(1,n+1): | ||
for j in range(m): | ||
now, next, cost = edges[j][0], edges[j][1], edges[j][2] | ||
if dist[now] != INF and dist[next] > dist[now] + cost: | ||
dist[next] = dist[now] + cost | ||
if i == n : | ||
return True | ||
return False | ||
|
||
n,m = map(int, input().split()) | ||
edges = [] | ||
dist = [INF] * (n+1) | ||
|
||
for _ in range(m): | ||
a,b,c = map(int, input().split()) | ||
edges.append((a,b,c)) | ||
|
||
negative_cycle = bellman_ford(1) | ||
if negative_cycle : | ||
print(-1) | ||
else : | ||
for i in range(2,n+1): | ||
if dist[i] == INF: | ||
print(-1) | ||
else : | ||
print(dist[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
N,M = map(int, input().split()) | ||
num_list = list(map(int, input().split())) | ||
accumulate_list = [0] * (N+1) | ||
for i in range(N): | ||
accumulate_list[i+1] = accumulate_list[i] + num_list[i] | ||
|
||
for _ in range(M): | ||
start, end = map(int, input().split()) | ||
print(accumulate_list[end] - accumulate_list[start-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys | ||
from heapq import * | ||
input = sys.stdin.readline | ||
T = int(input()) | ||
for _ in range(T): | ||
N = int(input()) | ||
chapters = list(map(int, input().split())) | ||
time_list = [] | ||
min_time=0 | ||
for chapter in chapters : | ||
heappush(time_list, chapter) | ||
while len(time_list) > 1 : | ||
temp =0 | ||
temp += heappop(time_list) + heappop(time_list) | ||
heappush(time_list, temp) | ||
min_time += temp | ||
print(min_time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
from heapq import * | ||
input = sys.stdin.readline | ||
|
||
def dijkstra(graph, start): | ||
length = [float('inf') for _ in graph] | ||
length[start] = 0 | ||
queue = [] | ||
heappush(queue, [length[start], start]) | ||
|
||
while queue: | ||
current_length, current_target = heappop(queue) | ||
|
||
if length[current_target] < current_length : | ||
continue | ||
|
||
for new_target, new_length in graph[current_target] : | ||
len = current_length + new_length | ||
if len < length[new_target]: | ||
length[new_target] = len | ||
heappush(queue, [len, new_target]) | ||
return length | ||
|
||
items = [] | ||
N,M,R = map(int, input().split()) | ||
T = list(map(int, input().split())) | ||
graph = [[] for _ in range(N+1)] | ||
|
||
for _ in range(R): | ||
start, end, weight = map(int, input().split()) | ||
graph[start].append((end, weight)) | ||
graph[end].append((start, weight)) | ||
|
||
for item in range(1, len(graph)) : | ||
result = dijkstra(graph, item) | ||
get = 0 | ||
for i in range(len(result)): | ||
if result[i] <= M : | ||
get += T[i-1] | ||
items.append(get) | ||
|
||
print(max(items)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sys | ||
from collections import deque | ||
input = sys.stdin.readline | ||
|
||
dx = [-1,-1,-1,0,0,1,1,1] | ||
dy = [-1,0,1,-1,1,-1,0,1] | ||
|
||
N,M,K = map(int, input().split()) | ||
A = [list(map(int, input().split())) for _ in range(N)] | ||
trees = [[deque() for _ in range(N)] for _ in range(N)] | ||
|
||
for _ in range(M) : | ||
x,y,z = map(int, input().split()) | ||
trees[x-1][y-1].append(z) | ||
|
||
ground = [[5] * N for _ in range(N)] | ||
for _ in range(K) : | ||
|
||
for i in range(N) : | ||
for j in range(N): | ||
trees_length = len(trees[i][j]) | ||
for k in range(trees_length) : | ||
if ground[i][j] >= trees[i][j][k] : | ||
ground[i][j] -= trees[i][j][k] | ||
trees[i][j][k] += 1 | ||
else : | ||
for _ in range(k,trees_length): | ||
ground[i][j] += trees[i][j].pop() // 2 | ||
break | ||
|
||
for i in range(N): | ||
for j in range(N) : | ||
for z in trees[i][j] : | ||
if z % 5 == 0: | ||
for idx in range(8): | ||
move_x = i + dx[idx] | ||
move_y = j + dy[idx] | ||
if 0 <= move_x < N and 0 <= move_y < N : | ||
trees[move_x][move_y].appendleft(1) | ||
ground[i][j] += A[i][j] | ||
|
||
answer = 0 | ||
for i in range(N) : | ||
for j in range(N): | ||
answer += len(trees[i][j]) | ||
print(answer) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
def power(A,B,C): | ||
if (B == 1) : return A%C | ||
else : | ||
temp = power(A,B//2, C) | ||
if (B%2 == 0) : return temp * temp % C | ||
else : return temp * temp * A % C | ||
|
||
A,B,C = map(int, input().split()) | ||
print(power(A,B,C)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
N = int(input()) | ||
lines = [list(map(int, input().rstrip().split())) for _ in range(N)] | ||
lines.sort() | ||
|
||
length =0 | ||
current_start = lines[0][0] | ||
current_end = lines[0][1] | ||
for start, end in lines[1:]: | ||
if start > current_end : | ||
print(start, current_end) | ||
length += (current_end - current_start) | ||
current_start = start | ||
current_end = max(current_end, end) | ||
length += (current_end - current_start) | ||
print(length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import sys, heapq | ||
input =sys.stdin.readline | ||
min_heap = [] | ||
max_heap = [] | ||
N = int(input()) | ||
for _ in range(N): | ||
number = int(input()) | ||
if (number > 0) : | ||
heapq.heappush(min_heap, number) | ||
elif (number < 0) : | ||
heapq.heappush(max_heap, -number) | ||
else : | ||
if min_heap : | ||
if max_heap : | ||
if min_heap[0] < max_heap[0] : | ||
print(heapq.heappop(min_heap)) | ||
else : | ||
print(-heapq.heappop(max_heap)) | ||
else : | ||
print(heapq.heappop(min_heap)) | ||
elif max_heap : | ||
print(-heapq.heappop(max_heap)) | ||
else : | ||
print(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
N,M = map(int, input().split()) | ||
word_count = {} | ||
for _ in range(N): | ||
word = input().rstrip() | ||
if len(word) >= M : | ||
if word in word_count : | ||
word_count[word][0] += 1 | ||
else : | ||
word_count[word] = [1,len(word)] | ||
|
||
print() | ||
for key in word_count : | ||
print (key) | ||
|
||
sorted_word_list = sorted(word_count, key=lambda key : (-word_count[key][0], -word_count[key][1], key)) | ||
print() | ||
for word in sorted_word_list : | ||
print(word) |