Skip to content

Commit f1f2b6b

Browse files
committed
[BOJ]1#14501_ํ‡ด์‚ฌ/ Silver / 60min
https://www.acmicpc.net/problem/14501
1 parent 30273e9 commit f1f2b6b

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
#dp : 1, ์ค‘๋ณต , ์ตœ์  ๋ถ€๋ถ„ -> memorize
3+
goal :์ตœ๋Œ€ ์ด์ต
4+
input : time , price , 0
5+
6+
#1. ์—ญ์ˆœ์œผ๋กœ N ~ 1 (ํ‡ด์‚ฌ์ผ N+1)
7+
for i in range(N , 0 , -1)
8+
#2-1 ์ƒ๋‹ด ๊ฐ€๋Šฅ ์—ฌ๋ถ€ (1)
9+
if i + T_i <= N ์ด๋ฉด -> ๊ฐ€๋Šฅ
10+
if not -> false (0)
11+
12+
# 2-2 ์ตœ๋Œ€ ํšจ์œจ
13+
๋ฆฌ์ŠคํŠธ dp :
14+
dp[i] = ํ˜„์žฌ price + ๋ฐ”๋กœ ๋‹ด ์ƒ๋‹ด price VS ๋‚ด์ผ ์ƒ๋‹ด price
15+
dp[i] = max(P[i] + dp[i+T_i] , P[i+1])
16+
"""
17+
18+
19+
20+
import sys
21+
n = int(sys.stdin.readline())
22+
schedules = [[]]
23+
for i in range(n) :
24+
t , p = map(int , sys.stdin.readline().split())
25+
schedules.append([t,p])
26+
27+
check = 0
28+
29+
dp = [0 for _ in range(n+2)]
30+
31+
for d in range(n ,0, -1 ) :
32+
t_i , p_i = schedules[d]
33+
#1. ์ƒ๋‹ด ๊ฐ€๋Šฅ์—ฌ๋ถ€
34+
if d + t_i > n+1 : # ์ƒ๋‹ด ๋ถˆ๊ฐ€๋Šฅ
35+
dp[d] = dp[d+1]
36+
else : #์ƒ๋‹ด ๊ฐ€๋Šฅ
37+
dp[d] = max(p_i + dp[d + t_i] , dp[d+1] )
38+
39+
print(dp[1])
40+
41+
42+
def train_epoch_boosting(self, model, previous_model = None):
43+
'''
44+
๋ถ€์ŠคํŒ…์„ ์ ์šฉํ•œ ํ•™์Šต (1 ์—ํญ)
45+
'''
46+
model.train()
47+
total_loss = 0.0
48+
acc_cum = 0
49+
progress_bar = tqdm(self.train_loader, desc='Training with Boosting')
50+
print(len(self.models), 'Exists' if previous_model is not None else 'None') # ๋‚˜์ค‘์— ์ง€์šธ ๋ถ€๋ถ„
51+
for i,(images, targets) in enumerate(progress_bar):
52+
images, targets = images.to(self.device), targets.to(self.device)
53+
self.optimizer.zero_grad()
54+
55+
# ํ˜„์žฌ ๋ชจ๋ธ ์˜ˆ์ธก
56+
print(f"Image.shape: {images.shape}")
57+
print(f"targets.shape: {targets.shape}")
58+
outputs = model(images)
59+
acc_cum += self.accuracy(outputs, targets)
60+
# ํ‹€๋ฆฐ ์˜ˆ์ธก์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜ ์ ์šฉwh
61+
if previous_model is not None: # ์ด์ „ ๋ชจ๋ธ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ. ์ฆ‰, Base ๋ชจ๋ธ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋งŒ Penalty ๊ณ„์‚ฐ ํ›„, ์ ์šฉ
62+
previous_model.eval()
63+
with torch.no_grad():
64+
prev_outputs = previous_model(images)
65+
print('์ด์ „ ๋ชจ๋ธ์˜ ์ •ํ™•๋„:',self.accuracy(prev_outputs, targets))
66+
print('ํ˜„์žฌ ๋ชจ๋ธ์˜ ์ •ํ™•๋„:', self.accuracy(outputs, targets))
67+
# penalty_weights = self.boost_weights(prev_outputs, targets)
68+
loss = self.loss_fn(outputs, targets) # * penalty_weights.to(self.device)
69+
# loss = loss.mean()
70+
else:
71+
loss = self.loss_fn(outputs, targets)
72+
73+
74+
loss.backward()
75+
self.optimizer.step()
76+
self.scheduler.step()
77+
total_loss += loss.item()
78+
progress_bar.set_postfix(loss=loss.item(), acc = acc_cum / (i+1))

0 commit comments

Comments
ย (0)