Skip to content

Commit 47f20ab

Browse files
authored
Merge pull request #55 from 24-Algorithm/minjeong
Minjeong / 8์›” 4์ฃผ์ฐจ / 5๋ฌธ์ œ
2 parents 20d99a3 + 155a323 commit 47f20ab

6 files changed

+156
-0
lines changed

โ€Ž.DS_Storeโ€Ž

0 Bytes
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
from itertools import combinations
3+
input = sys.stdin.readline
4+
5+
N, M = map(int, input().split()) # ๊ณ ๋ฆฌ ํšŒ์›์˜ ์ˆ˜ N, ์น˜ํ‚จ ์ข…๋ฅ˜์˜ ์ˆ˜ M
6+
likes = [] # ๊ฐ ํšŒ์›์˜ ์น˜ํ‚จ ์„ ํ˜ธ๋„
7+
for _ in range(N):
8+
likes.append(list(map(int, input().split())))
9+
# likes = [list(map(int, input().split())) for _ in range(N)]
10+
11+
max_like = 0 # ๊ณ ๋ฆฌ ํšŒ์›๋“ค์˜ ๋งŒ์กฑ๋„์˜ ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’
12+
for a, b, c in combinations(range(M), 3): # M๊ฐœ์˜ ์น˜ํ‚จ ์ข…๋ฅ˜ ์ค‘์—์„œ 3๊ฐ€์ง€๋ฅผ ์„ ํƒํ•˜๋Š” ๋ชจ๋“  ์กฐํ•ฉ
13+
tmp_like = 0 # ํ˜„์žฌ ์กฐํ•ฉ์— ๋Œ€ํ•œ ํšŒ์›๋“ค์˜ ์ด ๋งŒ์กฑ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ž„์‹œ ๋ณ€์ˆ˜
14+
for i in range(N): # ํšŒ์›๋ณ„๋กœ ๋งŒ์กฑ๋„ ๊ณ„์‚ฐ
15+
# ํšŒ์› i์˜ ์„ ํ˜ธ๋„ ์ค‘์—์„œ ์„ ํƒ๋œ 3๊ฐœ์˜ ์น˜ํ‚จ ์ค‘ ๊ฐ€์žฅ ๋†’์€ ์„ ํ˜ธ๋„๋ฅผ ์ฐพ๋Š”๋‹ค.
16+
tmp_like += max(likes[i][a], likes[i][b], likes[i][c])
17+
max_like = max(max_like, tmp_like)
18+
19+
print(max_like)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import copy
2+
import sys
3+
from collections import deque
4+
from itertools import combinations
5+
input = sys.stdin.readline
6+
7+
# ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์—ฐ๊ตฌ์†Œ ๋‚ด์— ํ™•์‚ฐ๋˜๋Š” ๊ฒƒ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ํ•˜๋Š” ํ•จ์ˆ˜
8+
def bfs(new_lab, viruses):
9+
N = len(new_lab)
10+
M = len(new_lab[0])
11+
queue = deque(viruses)
12+
visited = [[False for _ in range(M)] for _ in range(N)]
13+
14+
while queue:
15+
x, y = queue.popleft()
16+
for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)): # ์ธ์ ‘ํ•œ ๋„ค ๋ฐฉํ–ฅ ํ™•์ธ
17+
nx, ny = x + dx, y + dy
18+
# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ , ์•„์ง ํ™•์‚ฐ๋˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด
19+
if (0 <= nx < N and 0 <= ny < M) and new_lab[nx][ny] == 0 and not visited[nx][ny]:
20+
new_lab[nx][ny] = 2 # ๋ฐ”์ด๋Ÿฌ์Šค ํ™•์‚ฐ
21+
visited[nx][ny] = True # ๋ฐฉ๋ฌธ ํ‘œ์‹œ
22+
queue.append((nx, ny)) # ํ์— ์ถ”๊ฐ€
23+
24+
def get_safe_area(new_lab):
25+
cnt = 0
26+
for row in new_lab:
27+
cnt += row.count(0)
28+
return cnt
29+
#return sum(row.count(0) for row in new_lab)
30+
31+
def solve(N, M, lab):
32+
empty_spaces = [] # ๋นˆ ์นธ์˜ ์œ„์น˜๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ
33+
viruses = [] # ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ
34+
# ๋นˆ ๊ณต๊ฐ„๊ณผ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜ ์ €์žฅ
35+
for i in range(N):
36+
for j in range(M):
37+
if lab[i][j] == 0: # ๋นˆ ์นธ
38+
empty_spaces.append((i, j))
39+
if lab[i][j] == 2: # ๋ฐ”์ด๋Ÿฌ์Šค
40+
viruses.append((i, j))
41+
42+
max_safe_area = 0 # ์ตœ๋Œ€ ์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜
43+
44+
for walls in combinations(empty_spaces, 3):
45+
new_lab = copy.deepcopy(lab) # ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ lab ๋ณต์‚ฌ
46+
47+
# ๋ฒฝ ์„ค์น˜
48+
for x, y in walls:
49+
new_lab[x][y] = 1
50+
51+
# ๋ฐ”์ด๋Ÿฌ์Šค ํ™•์‚ฐ์‹œํ‚ด
52+
bfs(new_lab, viruses)
53+
54+
# ์˜์—ญ ํฌ๊ธฐ ๊ณ„์‚ฐ
55+
safe_area_cnt = get_safe_area(new_lab)
56+
# ํ˜„์žฌ ์กฐํ•ฉ์˜ safe_area๊ฐ€ max_safe_area๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐ’ ์—…๋ฐ์ดํŠธ
57+
max_safe_area = max(max_safe_area, safe_area_cnt)
58+
59+
return max_safe_area
60+
61+
62+
# ์ž…์ถœ๋ ฅ
63+
N, M = map(int, input().split())
64+
lab = [list(map(int, input().split())) for _ in range(N)]
65+
66+
print(solve(N, M, lab))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
dp = [0] * (N + 1)
6+
7+
# 1๋ถ€ํ„ฐ 7๊นŒ์ง€ ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜ ์ดˆ๊ธฐํ™”
8+
dp[1:8] = [1, 1, 2, 2, 1, 2, 1]
9+
10+
for i in range(8, N+1):
11+
dp[i] = min(dp[i-1]+1, dp[i-2]+1, dp[i-5]+1, dp[i-7]+1)
12+
13+
print(dp[N])
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, M = map(int, input().split()) # N: ์Šคํฌ๋ฆฐ ์นธ ์ˆ˜, M: ๋ฐ”๊ตฌ๋‹ˆ ํฌ๊ธฐ
5+
J = int(input()) # ๋–จ์–ด์ง€๋Š” ์‚ฌ๊ณผ์˜ ๊ฐœ์ˆ˜
6+
apple_pos = [] # ๋–จ์–ด์ง€๋Š” ์‚ฌ๊ณผ์˜ ์œ„์น˜
7+
for _ in range(J):
8+
apple_pos.append(int(input()))
9+
10+
# ๋ฐ”๊ตฌ๋‹ˆ ์ดˆ๊ธฐ ์œ„์น˜: ์Šคํฌ๋ฆฐ ๊ฐ€์žฅ ์™ผ์ชฝ
11+
basket_left = 1
12+
basket_right = M
13+
distance = 0 # ์ด๋™๊ฑฐ๋ฆฌ
14+
15+
for apple in apple_pos:
16+
# ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์‚ฌ๊ณผ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์— ์žˆ๋Š” ๊ฒฝ์šฐ
17+
if basket_left <= apple <= basket_right:
18+
continue
19+
# ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์‚ฌ๊ณผ์˜ ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฒฝ์šฐ
20+
elif basket_left < apple:
21+
distance += apple - basket_right
22+
basket_right = apple
23+
basket_left = apple - M + 1
24+
# ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์‚ฌ๊ณผ์˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ฒฝ์šฐ
25+
elif apple < basket_right:
26+
distance += basket_left - apple
27+
basket_left = apple
28+
basket_right = apple + M - 1
29+
30+
print(distance)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
5+
def gcd(a, b):
6+
while b != 0:
7+
a, b = b, a % b
8+
return a
9+
10+
# DP ๋ฐฐ์—ด ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”
11+
dp = [0 for _ in range(1001)]
12+
dp[1] = 3
13+
14+
# DP ๋ฐฐ์—ด ๊ฐ’ ์ฑ„์šฐ๊ธฐ
15+
for x in range(2, 1001):
16+
cnt = 0
17+
for y in range(1, x):
18+
if x == y:
19+
continue
20+
if gcd(x, y) == 1: # ์„œ๋กœ์†Œ๋ผ๋ฉด, (x,y)์™€ (y,x)๊ฐ€ ์›์ ์—์„œ ๋ณด์ด๋ฏ€๋กœ +2
21+
cnt += 2
22+
dp[x] = dp[x - 1] + cnt
23+
24+
# ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ฒ˜๋ฆฌ
25+
C = int(input()) # ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜
26+
for _ in range(C):
27+
N = int(input())
28+
print(dp[N])

0 commit comments

Comments
ย (0)