Skip to content

Commit 4c8f16a

Browse files
committed
2 parents 37cbaba + feb52ea commit 4c8f16a

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
조건
3+
1. 전투력 -> 내림차순 부분수열
4+
2. 남은 병사들의 수가 최대 = 열외 병사수 최소
5+
# LIS , Dynamic
6+
goal : 남은 병사들의 수가 최대 , 열외하는 병사 수 출력
7+
'''
8+
import sys
9+
10+
nums =int(sys.stdin.readline()) # 수열 길이
11+
soldiers =list( map(int,sys.stdin.readline().split())) # 주어진 수열
12+
13+
# DP 테이블 = 1 로 초기화
14+
dp= [ 1 for _ in range(nums)]
15+
16+
# 순서를 뒤집어서 'LIS - 최장 증가 부분 수열' 문제로 변환
17+
soldiers.reverse() # goal : 내림차순 -> 오름차순
18+
19+
# 가장 긴 오른차순 부분 수열 (LIS) 알고리즘 수행
20+
21+
for i in range(1, len(soldiers)) :
22+
for j in range(0,i) :
23+
if soldiers[j] < soldiers[i] : # 오름차순 만족
24+
dp[i] = max(dp[j]+1 , dp[i]) # i번 병사가 열외 x , 열외 o
25+
26+
# 열외하는 병사 최소 수
27+
print(nums - max(dp))

Hongjoo/백준/퇴사.py

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))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from itertools import product
2+
3+
def solution(word):
4+
answer = 0
5+
c_list = ['A', 'E', 'I', 'O', 'U']
6+
dictionary = []
7+
8+
for i in range(1, len(c_list) + 1):
9+
for j in product(c_list, repeat=i):
10+
dictionary.append(''.join(j))
11+
dictionary.sort()
12+
13+
return dictionary.index(word) + 1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 초기화
5+
N = int(input()) # 남은 날짜
6+
t = [0] * (N+1) # 걸리는 기간 리스트
7+
p = [0] * (N+1) # 상담 금액 리스트
8+
dp = [0] * (N+1) # DP
9+
for i in range(1, N+1):
10+
t[i], p[i] = map(int, input().split())
11+
12+
# DP 채우기
13+
for i in range(1, N+1):
14+
time, pay = t[i], p[i]
15+
day = i + time - 1
16+
dp[i] = max(dp[i], dp[i-1])
17+
if day <= N:
18+
dp[day] = max(dp[day], dp[i - 1] + pay)
19+
print(dp[-1])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import heapq
2+
3+
def solution(routes):
4+
routes.sort(key=lambda x:x[0])
5+
6+
answer = 0
7+
h = [30001]
8+
for r in routes:
9+
earliestend = heapq.heappop(h)
10+
if r[0] > earliestend:
11+
answer += 1
12+
h.clear()
13+
else:
14+
heapq.heappush(h, earliestend)
15+
heapq.heappush(h, r[1])
16+
else:
17+
if len(h) != 0:
18+
answer += 1
19+
return answer

0 commit comments

Comments
 (0)