Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 재귀
- 구현
- 우선순위큐
- 크루스칼
- 트리
- DFS
- 플로이드-워셜
- 브루트포스
- 다익스트라
- BFS
- 서브쿼리
- 다이나믹프로그래밍
- 시뮬레이션
- 에라토스테네스의 체
- 그래프 탐색
- 자료구조
- DP
- 누적합
- 그리디
- GROUP BY
- 백트래킹
- 다이나믹 프로그래밍
- 해시
- 투포인터
- 수학
- 다시
- MST
- join
- 그래프 이론
- 분할정복
Archives
- Today
- Total
기록하고 까먹지 말기
1744 본문
날짜 : 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일 때의 조건에 따라 분리하여 곱하고, 더하는 방식으로 문제를 풀이했다.
참고 사이트
-