E. Hanging Hearts
E. Hanging Hearts
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 .
Input
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.
Output
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.
Examples
input
6 1 2 1 4 2
output
4
input
2 1
output
2
Note
The following is the structure of the cards in the first example.
Pak Chanek can choose the permutation .
Let be the number written on card . Initially, . Pak Chanek can do the following operations in order:
- Select card . Append to the end of . As , the value of becomes . Remove card . After this operation, .
- Select card . Append to the end of . As , the value of is left unchanged. Remove card . After this operation, .
- Select card . Append to the end of . As , the value of is left unchanged. Remove card . After this operation, .
- Select card . Append to the end of . As , the value of becomes . Remove card . After this operation, .
- Select card . Append to the end of . As , the value of is left unchanged. Remove card . After this operation, .
- Select card . Append to the end of . Remove card . After this operation, .
One of the longest non-decreasing subsequences of is . Thus, the length of the longest non-decreasing subsequence of is . It can be proven that this is indeed the maximum possible length.
解题思路
关键性质,对于某棵子树,得到的序列的最后一个元素必然是该子树的根节点,同时这个值是整棵子树中的最小值。
对于最优解,对应的树的父节点的值一定比子节点的值都要大。否则如果一颗树存在某个子树的根节点比它的某个儿子的值要小,那么我们就交换两个节点的值,在得到的序列中这个子节点所对应的值一定会变小,而根节点的值不变,因此得到的结果不会变差,即最长非递减子序列只会变长而不会变短。因此只要存在某个父节点比子节点的值要大,那么就交换,最后整棵树的父节点都会比子节点要大。
因此对于某棵子树所得到的所有序列我们可以根据是否选择这个子树的根(也就是序列的最后一个元素)来分成两大类。定义表示以为根的子树中,不选择根节点的所有序列;表示以为根的子树中,选择根节点的所有序列。属性是序列的非递减子序列长度的最大值。
如果选择序列中包含根节点,由于此时根节点的值是整棵子树中的最小值,因此最长的非递减子序列的值必然也是整棵子树中的最小值,因此我们可以贪心地构造,找到这棵子树从根节点开始的最长路径,然后这条路径的叶子节点定义为整棵子树的最小值,那么非递减子序列长度的最大值就是整棵子树的最长路径。。
如果选择序列中不包含根节点,对于某棵子树的根节点,以其各个子节点为根的子树所得到的序列都是相互独立的,因为它们之间的删除顺序不会互相干扰,因此最后所有以各个子节点为根的子树所得到的序列是可以任意合并的,同时我们也可以单独来求各个以子节点为根的子树的非递减子序列。由于可以任意组合各个子树所得到的序列,因此我们可以贪心地来按照非递减的方式来合并以得到最长的非递减子序列。。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 1e5 + 10; 5 6 int head[N], e[N], ne[N], idx; 7 int f[N][2]; 8 9 void add(int v, int w) { 10 e[idx] = w, ne[idx] = head[v], head[v] = idx++; 11 } 12 13 void dfs(int u) { 14 f[u][1] = 1; 15 for (int i = head[u]; i != -1; i = ne[i]) { 16 dfs(e[i]); 17 f[u][0] += max(f[e[i]][0], f[e[i]][1]); 18 f[u][1] = max(f[u][1], f[e[i]][1] + 1); 19 } 20 } 21 22 int main() { 23 int n; 24 scanf("%d", &n); 25 memset(head, -1, sizeof(head)); 26 for (int i = 2; i <= n; i++) { 27 int p; 28 scanf("%d", &p); 29 add(p, i); 30 } 31 dfs(1); 32 printf("%d", max(f[1][0], f[1][1])); 33 34 return 0; 35 }
参考资料
Codeforces Round #831 (Div. 1 + Div. 2) A~F:https://zhuanlan.zhihu.com/p/579511116
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16904820.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效