[Solution] Hanging Hearts Codeforces Solution
Pak Chanek has blank heart-shaped cards. Card is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card () is hanging onto card ().
In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation of . Then, the number written on card is .
After that, Pak Chanek must do the following operation times while maintaining a sequence (which is initially empty):
- Choose a card such that no other cards are hanging onto it.
- Append the number written on card to the end of .
- If and the number on card is larger than the number on card , replace the number on card with the number on card .
- Remove card .
After that, Pak Chanek will have a sequence with elements. What is the maximum length of the longest non-decreasing subsequence of at the end if Pak Chanek does all the steps optimally?
A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements. For example, is a subsequence of , and , but not and .
The first line contains a single integer () — the number of heart-shaped cards.
The second line contains integers () describing which card that each card hangs onto.
Print a single integer — the maximum length of the longest non-decreasing subsequence of at the end if Pak Chanek does all the steps optimally.
No comments:
Post a Comment