Skip to content

Commit a1d679a

Browse files
committed
백준 7453번 합이 0인 네 정수
문제 링크: https://www.acmicpc.net/problem/7453
1 parent dd4798a commit a1d679a

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
3+
# pypy3를 선택해야 함, python3로 하면 시간 초과 발생
4+
def solve():
5+
input = sys.stdin.readline
6+
n = int(input().strip())
7+
dts = [list(map(int, input().split())) for _ in range(n)]
8+
ABs, CDs = [], []
9+
10+
for i in range(n):
11+
for j in range(n):
12+
ab = dts[i][0] + dts[j][1]
13+
cd = dts[i][2] + dts[j][3]
14+
ABs.append(ab)
15+
CDs.append(cd)
16+
17+
ABs.sort()
18+
CDs.sort()
19+
20+
answer = 0
21+
22+
idx_ab = 0
23+
idx_cd = len(CDs) - 1
24+
25+
while idx_ab < len(ABs) and 0 <= idx_cd:
26+
v = ABs[idx_ab] + CDs[idx_cd]
27+
28+
if v == 0:
29+
idx_ab2 = idx_ab + 1
30+
# 같은 값이 반복되는 경우 갯수 새기
31+
while idx_ab2 < len(ABs) and ABs[idx_ab] == ABs[idx_ab2]:
32+
idx_ab2 += 1
33+
ab_len = idx_ab2 - idx_ab
34+
35+
idx_cd2 = idx_cd - 1
36+
while 0 <= idx_cd2 and CDs[idx_cd] == CDs[idx_cd2]:
37+
idx_cd2 -= 1
38+
cd_len = idx_cd - idx_cd2
39+
40+
answer += (ab_len * cd_len)
41+
42+
idx_ab = idx_ab2
43+
idx_cd = idx_cd2
44+
elif v < 0:
45+
idx_ab += 1
46+
else: # v > 0:
47+
idx_cd -= 1
48+
49+
print(answer)
50+
51+
52+
if __name__ == '__main__':
53+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
6
2+
-45 22 42 -16
3+
-41 -27 56 30
4+
-36 53 -37 77
5+
-36 30 -75 -46
6+
26 -38 -10 62
7+
-32 -54 -6 45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)