LA FORET ROUGE

[PS] Baekjoon #12018

⏱ 1m | Categories: ALGORITHM | Tags: BAEKJOON , GREEDY

Baekjoon #12018 Yonsei TOTO

Conditions

  • Set 1 to 36 mileage to the subject you wish to take.
  • After all, the number of students will be decided in descending order of mileage.
  • Output is the maximum number of subjects available for the given mileage.
  • If the mileage is same, you are the priority.

Strategy

  • If the applicant is less than the number of available -> Only 1 mileage can be taken.
  • Else, equal to or larger, use the same value as the mileage of last available person. -> Because of the priority.
  • Make sure you have enough mileage before each application.
 1import sys
 2n, m = map(int, sys.stdin.readline().split())
 3
 4lectures = []
 5for i in range(n):
 6    temp = {}
 7    temp['p'], temp['l'] = map(int, sys.stdin.readline().split())
 8    temp['mi'] = list(map(int, sys.stdin.readline().split()))
 9    temp['sorted_mi'] = sorted(temp['mi'], reverse=True)[:temp['l']]
10    temp['min_mi'] = temp['sorted_mi'][-1]
11    lectures.append(temp)
12
13cnt = 0
14del_list = []
15for temp in lectures:
16    if temp['p'] < temp['l']:
17        if m < 1:               # Check left mileage
18            continue
19        cnt += 1
20        m -= 1
21        del_list.append(lectures.index(temp))
22
23for k in del_list[::-1]:
24    lectures.pop(k)
25
26sorted_lecture = sorted(lectures, key=lambda x: x['min_mi'])
27for temp in sorted_lecture:
28    if m < temp['min_mi']:
29        continue
30    cnt += 1
31    m -= temp['min_mi']
32
33print(cnt)

Comments

Link copied to clipboard!