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 sys
2+
3+ input = sys .stdin .readline
4+
5+ n = int (input ())
6+ towers = list (map (int , input ().split ()))
7+
8+ stack = [] # 탑 번호
9+ res = [0 ] * n
10+
11+ for i in range (n ):
12+ while stack and towers [stack [- 1 ]] < towers [i ]: # 왼쪽 탑이 더 낮으면
13+ stack .pop ()
14+ if stack :
15+ res [i ] = stack [- 1 ] + 1
16+ stack .append (i )
17+ print (* res )
18+
Original file line number Diff line number Diff line change 1+ """
2+ dp[현재노드] = min(dp[자식노드][얼리어답터인 경우;1], dp[자식노드][아닌 경우;0])
3+ """
4+
5+ import sys
6+ from collections import *
7+ sys .setrecursionlimit (10 ** 6 )
8+
9+ input = sys .stdin .readline
10+
11+ graph = defaultdict (list )
12+ n = int (input ())
13+ for _ in range (n - 1 ):
14+ u , v = map (int , input ().split ())
15+ graph [u ].append (v )
16+ graph [v ].append (u )
17+
18+ dp = [[0 , 0 ] for _ in range (n + 1 )] # [노드번호][얼리어답터 체크]
19+ visited = [0 for _ in range (n + 1 )]
20+ def dfs (start ):
21+ visited [start ] = 1
22+ if len (graph [start ]) == 0 :
23+ dp [start ][1 ] = 1
24+ dp [start ][0 ] = 0
25+ else :
26+ for i in graph [start ]:
27+ if not visited [i ]:
28+ dfs (i )
29+ dp [start ][1 ] += min (dp [i ][0 ], dp [i ][1 ])
30+ dp [start ][0 ] += dp [i ][1 ]
31+ dp [start ][1 ] += 1
32+
33+ dfs (1 )
34+ print (min (dp [1 ][0 ], dp [1 ][1 ]))
You can’t perform that action at this time.
0 commit comments