PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000252. Dãy chướng ngại vật hợp lệ dài nhất tại mỗi vị trí (Find the Longest Valid Obstacle Course at Each Position)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Dãy chướng ngại vật hợp lệ dài nhất tại mỗi vị trí (Find the Longest Valid Obstacle Course at Each Position)
Find the Longest Valid Obstacle Course at Each Position
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
There are obstacles in left-to-right order, numbered from to . Obstacle has height .
For each position , choose a subsequence of obstacles from positions satisfying:
- Position must be selected and must be the last selected obstacle.
- Selected positions are strictly increasing (they need not be consecutive).
- Heights of selected obstacles are nondecreasing from left to right. Equal heights are allowed.
Let be the maximum number of selected obstacles when the subsequence must end at position . Output all .
Input
- The first line contains an integer , the number of obstacles.
- The second line contains exactly integers , the obstacle heights in position order.
Output
Print integers on one line, separated by spaces.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
4
1 2 3 2
Output
1 2 3 3
Explanation
The heights are . Consider each required ending position:
- : choose , giving .
- : choose , giving .
- : choose , giving .
- : height cannot follow height because that would decrease. But positions give heights , a valid course of length . Thus .
The output is 1 2 3 3.