Skip to content

Commit 53b36be

Browse files
authored
Merge pull request #142 from YoonYn9915/main
YoonYn9915/ 2월 2주차 / 3문제
2 parents 751ea44 + 7027d21 commit 53b36be

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import heapq
2+
3+
n = int(input())
4+
5+
hq = []
6+
for i in range(n):
7+
votes = int(input())
8+
if i == 0:
9+
dasom_votes = votes
10+
continue
11+
12+
heapq.heappush(hq, -votes)
13+
14+
bribe_count = 0
15+
while hq:
16+
votes = -heapq.heappop(hq)
17+
if votes < dasom_votes:
18+
break
19+
20+
dasom_votes += 1
21+
bribe_count += 1
22+
heapq.heappush(hq, -(votes - 1))
23+
24+
print(bribe_count)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import math
2+
from collections import defaultdict
3+
4+
N, M, K = map(int, input().split())
5+
6+
# 파이어볼을 나타내는 배열, 순서대로 (x위치, y위치, 질량, 속력, 방향)
7+
fire_balls = []
8+
9+
ans = 0
10+
11+
# 방향을 나타내는 배열
12+
dir = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
13+
14+
# 파이어볼을 위치별로 묶어두기 위한 딕셔너리
15+
fire_balls_position = defaultdict(list)
16+
17+
# 파이어볼 위치 기록 (위치별 파이어볼 목록)
18+
for _ in range(M):
19+
r, c, m, s, d = map(int, input().split())
20+
fire_balls.append((r, c, m, s, d))
21+
22+
# K번 만큼 파이어볼 이동
23+
for _ in range(K):
24+
# 새 위치로 이동한 파이어볼을 임시로 기록
25+
fire_balls_position.clear()
26+
27+
for r, c, m, s, d in fire_balls:
28+
# 파이어볼을 방향과 속력에 따라 이동
29+
dx, dy = dir[d]
30+
r = ((r + dx * s - 1) % N) + 1
31+
c = ((c + dy * s - 1) % N) + 1
32+
33+
# 새로운 위치에 파이어볼을 추가
34+
fire_balls_position[(r, c)].append((r, c, m, s, d))
35+
36+
# 파이어볼 합치기
37+
fire_balls.clear()
38+
for (r, c), balls in fire_balls_position.items():
39+
if len(balls) > 1: # 한 위치에 2개 이상 파이어볼이 있을 경우
40+
# 질량, 속력, 방향 계산
41+
m_sum = sum(ball[2] for ball in balls)
42+
s_sum = sum(ball[3] for ball in balls)
43+
count = len(balls)
44+
45+
# 짝수, 홀수 방향 판별
46+
flag = -1
47+
for _, _, _, _, d in balls:
48+
if flag == -1:
49+
flag = d
50+
elif flag % 2 != d % 2:
51+
flag = -10 # 홀수, 짝수 혼합
52+
53+
# 질량이 0보다 크면 4개로 나눈다
54+
if m_sum // 5 > 0:
55+
if flag == -10:
56+
fire_balls.extend([(r, c, m_sum // 5, s_sum // count, i) for i in [1, 3, 5, 7]])
57+
else:
58+
fire_balls.extend([(r, c, m_sum // 5, s_sum // count, i) for i in [0, 2, 4, 6]])
59+
else:
60+
# 1개일 경우 그대로 남긴다
61+
fire_balls.extend(balls)
62+
63+
# 결과 출력
64+
ans = sum(m for _, _, m, _, _ in fire_balls)
65+
print(ans)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 모래 계산하는 함수
2+
def recount(time, dx, dy, direction):
3+
global ans, s_x, s_y
4+
5+
# y좌표 계산 & x좌표 갱신
6+
for _ in range(time):
7+
s_x += dx
8+
s_y += dy
9+
if s_y < 0: # 범위 밖이면 stop
10+
break
11+
12+
# 3. a, out_sand
13+
total = 0 # a 구하기 위한 변수
14+
for dx, dy, z in direction:
15+
nx = s_x + dx
16+
ny = s_y + dy
17+
if z == 0: # a(나머지)
18+
new_sand = sand[s_x][s_y] - total
19+
else: # 비율
20+
new_sand = int(sand[s_x][s_y] * z)
21+
total += new_sand
22+
23+
if 0 <= nx < N and 0 <= ny < N: # 인덱스 범위이면 값 갱신
24+
sand[nx][ny] += new_sand
25+
else: # 범위 밖이면 ans 카운트
26+
ans += new_sand
27+
28+
29+
N = int(input())
30+
sand = [list(map(int, input().split())) for _ in range(N)]
31+
32+
# 2. 방향별 모래 비율 위치
33+
left = [(1, 1, 0.01), (-1, 1, 0.01), (1, 0, 0.07), (-1, 0, 0.07), (1, -1, 0.1),
34+
(-1, -1, 0.1), (2, 0, 0.02), (-2, 0, 0.02), (0, -2, 0.05), (0, -1, 0)]
35+
right = [(x, -y, z) for x, y, z in left]
36+
down = [(-y, x, z) for x, y, z in left]
37+
up = [(y, x, z) for x, y, z in left]
38+
39+
s_x, s_y = N//2, N//2 # 시작좌표(x좌표)
40+
ans = 0 # out_sand
41+
42+
# 1.토네이도 회전 방향(y위치)
43+
for i in range(1, N + 1):
44+
if i % 2:
45+
recount(i, 0, -1, left)
46+
recount(i, 1, 0, down)
47+
else:
48+
recount(i, 0, 1, right)
49+
recount(i, -1, 0, up)
50+
51+
print(ans)

0 commit comments

Comments
 (0)