Skip to content

Commit b8f3db6

Browse files
committed
git commit -m "[BOJ]#12865평범한배낭/ Gold5/ 실패" -m "https://www.acmicpc.net/problem/12865"
1 parent e6ff184 commit b8f3db6

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Hongjoo/백준/평범한배낭.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
N :물건 개수 , K : 제한 무게
3+
W , V : 개별 물건 무게 , 가지
4+
goal: max V
5+
6+
6 4 3 5
7+
13 8 6 12
8+
9+
-> 13/6 ,8/4 , 6/3 , 5/2
10+
goal : 낮은 무게부터 넣기
11+
[[3, 6], [4, 8], [5, 12], [6, 13]]
12+
1. sum(W) <= K인 조합 찾기
13+
2. w확인?
14+
"""
15+
#1. 변수 입력 받기
16+
import sys
17+
objects = list()
18+
N , K = map(int,sys.stdin.readline().split() )
19+
for i in range(N) :
20+
W , V = map(int,sys.stdin.readline().split() )
21+
objects.append([W,V])
22+
23+
# 가벼운 순으로 나열
24+
objects = sorted(objects)
25+
# print(objects)
26+
bags = list()
27+
#2. two point
28+
# start + window
29+
# for start in range(len(objects)) :
30+
# for end in range(1,len(objects)) : # end point
31+
start = 0 ; end= 1
32+
max_values = 0
33+
# 물건 1개의 무게 < target weight
34+
35+
while start < N and start < end and objects[end-1][0] <= K :
36+
37+
Subset = objects[start : end] # length = start -end
38+
currnet_weight = 0 ; current_values = 0
39+
# print(Subset)
40+
41+
for w,v in Subset :
42+
currnet_weight += w
43+
current_values += v
44+
45+
if currnet_weight <= K :# K 보다 현재 배낭 무게가 가벼우면 업데이트 진행
46+
max_values = max(max_values , current_values)
47+
end += 1
48+
elif currnet_weight <= K or end > N:# current_weight > k 이거나 end == N 이면 초기화
49+
current_values = 0
50+
start += 1
51+
end = start +1
52+
# print("end",end )
53+
# if end > N:
54+
# start += 1
55+
# end = start +1
56+
# # print(f"update { max_values}")
57+
print(max_values)

0 commit comments

Comments
 (0)