File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import heapq
2+ import sys
3+ from collections import defaultdict
4+
5+
6+ def main ():
7+ input = sys .stdin .readline
8+
9+ N , M = map (int , input ().split ())
10+
11+ problems = defaultdict (list )
12+ aheadResolveProblems = [0 ] * (N + 1 )
13+
14+ for i in range (M ):
15+ A , B = map (int , input ().split ())
16+ problems [A ].append (B )
17+ aheadResolveProblems [B ] += 1
18+
19+ result = []
20+ heap = []
21+
22+ for i in range (1 , N + 1 ):
23+ if aheadResolveProblems [i ] == 0 :
24+ heapq .heappush (heap , i )
25+
26+ while heap :
27+ now = heapq .heappop (heap )
28+ result .append (now )
29+
30+ for next in problems [now ]:
31+ aheadResolveProblems [next ] -= 1
32+ if aheadResolveProblems [next ] == 0 :
33+ heapq .heappush (heap , next )
34+
35+ print (* result )
36+
37+ if __name__ == '__main__' :
38+ main ()
39+
40+ # 1234
41+ # 1. 4는 2보다 먼저 푸는 것이 좋음 -> 1 4 2 3
42+ # 2. 3은 1보다 먼저 푸는 것이 좋음 -> 3 1 4 2
Original file line number Diff line number Diff line change 1+ import sys
2+
3+ def main ():
4+ input = sys .stdin .readline
5+
6+ N , S = map (int , input ().split ())
7+ sequence = list (map (int , input ().split ()))
8+
9+ start = 0
10+ end = 0
11+ current_sum = 0
12+ result = float ('inf' )
13+
14+ while True :
15+ if current_sum >= S :
16+ result = min (result , end - start )
17+ current_sum -= sequence [start ]
18+ start += 1
19+ elif end == N :
20+ break
21+ else :
22+ current_sum += sequence [end ]
23+ end += 1
24+
25+ print (result if result != float ('inf' ) else 0 )
26+
27+ if __name__ == '__main__' :
28+ main ()
You can’t perform that action at this time.
0 commit comments