전공/백준
1744
yha97
2022. 10. 26. 11:39
날짜 : 2022. 10. 26
사용 언어 : python
문제
코드
import sys
n = int(sys.stdin.readline()) # 수열의 크기
po = [] # 양수
ne = [] # 음수
for i in range(n):
t = int(sys.stdin.readline())
if t > 0 :
po.append(t)
else:
ne.append(t)
po.sort(reverse=True)
ne.sort()
#print(po)
#print(ne)
res_p = 0
res_n = 0
temp = []
while po:
t = po.pop(0)
if t == 1: # 1이면 그냥 더함
res_p += t
else: # 1이 아닌 수끼리 곱함
temp.append(t)
if len(temp) == 2:
res_p += (temp[0] * temp[1])
temp = []
if len(temp) > 0:
for i in temp:
res_p += i
temp = []
while ne:
t1 = ne.pop(0)
temp.append(t1) # 음수끼리 곱함
if len(temp) == 2:
res_n += temp[0] * temp[1]
temp = []
if len(temp) > 0:
for i in temp:
res_n += i
print(res_n + res_p)
알게된 점
- 그리디 문제였고, 양수와 음수, 1일 때, 0일 때의 조건에 따라 분리하여 곱하고, 더하는 방식으로 문제를 풀이했다.
참고 사이트
-