1084 D
题意
给你一棵树,有点权、边权。找出一条路径,对于这条路径序列上的任意 $ile $ 路径长度,这条路径上所有点权 (v_i) 的和 $ge $ 所有边权 (e_i) 的和。输出最大可能的 (sum v_i-sum e_i) 的值。((1≤n≤3⋅10^5))
Examples
Input
3
1 3 3
1 2 2
1 3 2
Output
3
Input
5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1
Output
7
解
实际上只需要用树形dp求出值最大的路径即可,无需关心某一条路径是否合法。原理同“最大子段和”。
转移方程:(dp[u]=max {dp[v_i]+a[u],dp[v_i]+dp[v_j]+a[u],a[u]}(i≠j))
1084 E
题意
由'a','b'
组成的两个字符串 (s_1,s_2) ,长度都为 (n) 。在 (s_1le xle s_2) 中的所有字符串 (x) 中选出 (k) 个(此处 $le $ 为字典序比较),求这 (k) 个字符串中不同前缀的最大值。((1≤n≤5⋅10^5,1≤k≤10^9))
Examples
Input
2 4
aa
bb
Output
6
Input
3 3
aba
bba
Output
8
Input
4 5
abbb
baaa
Output
8
解
考虑dp。
(dp[i]) 表示在所有字符串 (x) 中合法的长度为 (i) 的前缀有多少个。有点像数位dp。
转移:
(dp[i]=dp[i-1]*2,s_1[i]='a' and s_2[i]='b')
(dp[i]=dp[i-1]*2-1,s_1[i]='a' and s_2[i]='b') 或者 (s_1[i]='b' and s_2[i]='a')
(dp[i]=dp[i-1]*2-2,s_1[i]='b' and s_2[i]='a')
实际上结果就是把两个字符串看成两个二进制数相减。
最后,别忘了每次求出后都要取(min(dp[i],k))。