Skip to content

Commit 300823b

Browse files
authored
Merge pull request #251 from AlgorithmStudy-Allumbus/minjeong6
Minjeong / 10์›” 3์ฃผ์ฐจ / 2๋ฌธ์ œ
2 parents 0d4387b + 62e726f commit 300823b

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# N: ์–ผ์Œ ์–‘๋™์ด ์ˆ˜, K: ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ
5+
N, K = map(int, input().split())
6+
buckets = []
7+
max_x = 0
8+
for _ in range(N):
9+
# g: ์–ผ์Œ ์–‘, x: ์–‘๋™์ด ์ขŒํ‘œ
10+
g, x = map(int, input().split())
11+
buckets.append((g, x))
12+
max_x = max(max_x, x)
13+
14+
arr = [0] * (max_x+1)
15+
for g, x in buckets:
16+
arr[x] += g
17+
18+
window_size = 2 * K + 1
19+
current = sum(arr[:window_size])
20+
answer = current
21+
for i in range(window_size, max_x + 1):
22+
current += arr[i] - arr[i - window_size]
23+
answer = max(answer, current)
24+
25+
print(answer)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
coding = list(map(int, input().split()))
6+
coding.sort()
7+
answer = 0
8+
9+
for i in range(N):
10+
target = -coding[i]
11+
left = i + 1
12+
right = N - 1
13+
14+
while left < right:
15+
two_sum = coding[left] + coding[right]
16+
if two_sum < target:
17+
left += 1
18+
elif two_sum > target:
19+
right -= 1
20+
else: # two_sum == target
21+
if coding[left] != coding[right]:
22+
left_value = coding[left]
23+
right_value = coding[right]
24+
25+
# ์™ผ์ชฝ์— ๊ฐ™์€ ๊ฐ’(left_value)์ด ์—ฐ์†์œผ๋กœ ๋ช‡ ๋ช… ์žˆ๋Š”์ง€ ์„ผ๋‹ค
26+
cnt_left = 1
27+
while left + cnt_left < right and coding[left + cnt_left] == left_value:
28+
cnt_left += 1
29+
30+
# ์˜ค๋ฅธ์ชฝ์— ๊ฐ™์€ ๊ฐ’(right_value)์ด ์—ฐ์†์œผ๋กœ ๋ช‡ ๋ช… ์žˆ๋Š”์ง€ ์„ผ๋‹ค
31+
cnt_right = 1
32+
while right - cnt_right > left and coding[right - cnt_right] == right_value:
33+
cnt_right += 1
34+
35+
# ์ด ์กฐํ•ฉ๋“ค ๊ฐ๊ฐ์€ ์ „๋ถ€ ์„œ๋กœ ๋‹ค๋ฅธ ํ•™์ƒ ์กฐํ•ฉ์ด๋ฏ€๋กœ
36+
# ๊ฒฝ์šฐ์˜ ์ˆ˜ = cnt_left * cnt_right
37+
answer += cnt_left * cnt_right
38+
39+
# ์„ธ์•ผ ํ•  ํ•™์ƒ์„ ์ฒ˜๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ, ํฌ์ธํ„ฐ ํ•œ ๋ฒˆ์— ์ด๋™
40+
left += cnt_left
41+
right -= cnt_right
42+
43+
else: # coding[left] == coding[right]
44+
# ์ด ๊ตฌ๊ฐ„์— ๋‚จ์€ ํ•™์ƒ ์ˆ˜
45+
m = right - left + 1
46+
47+
# m๋ช… ์ค‘ 2๋ช…์„ ๋ฝ‘๋Š” ์กฐํ•ฉ ์ˆ˜ C(m, 2)
48+
answer += m * (m - 1) // 2
49+
50+
# i์— ๋Œ€ํ•ด ์…€ ์กฐํ•ฉ ์ข…๋ฃŒ
51+
break
52+
53+
print(answer)

0 commit comments

Comments
ย (0)