当前位置:网站首页>【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
2022-08-05 03:51:00 【ZhgDgE】
#846. 第二大数字和
题意:给定长度为 n ( 2 ≤ n ≤ 1 0 5 ) n(2\leq n\leq 10^5) n(2≤n≤105) 的排列,问所有长度至少为 2 2 2 的子段次大值的和是多少。
题解:(数据结构) 代码源每日一题 Div1 第二大数字和
(O(n)做法) 代码源每日一题 Div1 第二大数字和
思路:对于子段问题,我们通常会从下标入手递推。但是 这道题子段次大值的问题是从值域的角度入手的 。
首先问题转化为:求每个数字 x x x 左右两边的第一个大于 x x x 的数和第二个大于 x x x 的数的下标,详见题解。
我们有一个有序数据结构,然后我们从大到小枚举每个数字,然后将当前数字 a i a_i ai 的下标 i i i 添加进去。添加之前计算第一大数和第二大数,在数据结构中这个下标右边的第一个数字,即为 a i a_i ai 在序列中右边第一个大于 a i a_i ai 的下标,第二大数同理。用 s e t set set 实现即可。
O ( n ) O(n) O(n) 的做法则是先构建 0 , 0 , 1 , 2 , ∼ n − 1 , n , n + 1 , n + 1 0,0,1,2,\sim n-1,n,n+1,n+1 0,0,1,2,∼n−1,n,n+1,n+1 的链表,然后从小到大枚举每个数字的下标,枚举完某个数之后在链表中删除该下标。这个数字的下标在链表中的左右两边的数字即为第一大数的下标,第二大数同理。
AC代码: O ( n log n ) O(n\log n) O(nlogn)http://oj.daimayuan.top/submission/314282
O ( n ) O(n) O(n)http://oj.daimayuan.top/submission/314318
#845. 石子游戏 III
题意:
题解:(博弈论)代码源每日一题 Div1 石子游戏 III
思路:循环论证。首先如果序列存在 0 0 0 ,那么:
- 当 n 2 < c o u n t ( 0 ) ≤ n \frac n 2 <count(0)\leq n 2n<count(0)≤n 时,为必输态(无法继续操作)。
- 当 0 < c o u n t ( 0 ) ≤ n 2 0<count(0)\leq \frac n 2 0<count(0)≤2n 时,为必胜态(可操作一次转为 1. )。
如果序列不存在 0 0 0 存在 1 1 1 ,那么:
- 当 n 2 < c o u n t ( 1 ) ≤ n \frac n 2 <count(1)\leq n 2n<count(1)≤n 时,为必输态(操作一次必然转为 2. )。
- 当 0 < c o u n t ( 1 ) ≤ n 2 0<count(1)\leq \frac n 2 0<count(1)≤2n 时,为必胜态(可操作一次转为 3. )。
类似于这样,如果最小值出现的次数 > n 2 >\frac n 2 >2n ,则为必输态,因为操作后新的最小值的次数一定是 ( 0 , n 2 ] (0,\frac n 2] (0,2n] 内的,即必胜态;反之为必胜态,因为可以通过选择非最小堆来使得最小值的堆数从 ( 0 , n 2 ] (0,\frac n 2] (0,2n] 变为 ( n 2 , n ] (\frac n 2, n] (2n,n] ,即必输态。
AC代码:http://oj.daimayuan.top/submission/313363
#850. 平衡二叉树
题意:平衡二叉树( AVL \text{AVL} AVL 树),是指左右子树高度差至多为 1 1 1 的二叉树,并且该树的左右两个子树也均为 AVL \text{AVL} AVL 树。 现在问题来了,给定 AVL \text{AVL} AVL 树的节点个数 n n n ,求有多少种形态的 AVL \text{AVL} AVL 树恰好有 n n n 个节点。共有 T T T 组询问,每次给定 n ( T , n ≤ 2000 ) n(T,n\leq 2000) n(T,n≤2000) 。
思路:定义 d p ( i , j ) dp(i,j) dp(i,j) 为有 i i i 个节点且高度为 j j j 的方案数,枚举左数的节点个数 k ( k ∈ [ 0 , i ] ) k(k\in[0,i]) k(k∈[0,i]) 。
d p ( i , j ) = d p ( k , j ) × d p ( i − k , j ) + d p ( k , j − 1 ) × d p ( i − k , j ) + d p ( k , j ) × d p ( i − k , j − 1 ) \begin{matrix} dp(i,j)=dp(k,j)\times dp(i-k,j) \\ ~~~~~~~~~~~~~~~~~~~~~~+dp(k,j-1)\times dp(i-k,j) \\ ~~~~~~~~~~~~~~~~~~~~~~+dp(k,j)\times dp(i-k,j-1) \end{matrix} dp(i,j)=dp(k,j)×dp(i−k,j) +dp(k,j−1)×dp(i−k,j) +dp(k,j)×dp(i−k,j−1)
刚开始以为 j j j 的取值范围是 [ 1 , n ] [1,n] [1,n] ,看了题解才想到 j j j 最大为 20 20 20 。时间复杂度 O ( 20 × n 2 ) O(20\times n^2) O(20×n2)
边栏推荐
- Redis1: Introduction to Redis, basic features of Redis, relational database, non-relational database, database development stage
- 【树莓派】树莓派调光
- 惨遭打脸:字节某部门竟有这么多测试员
- Fifteen. Actual combat - MySQL database building table character set and collation
- iMedicalLIS监听程序(2)
- YYGH-13-客服中心
- This year's Qixi Festival, "love vegetables" are more loving than gifts
- UE4 通过互动(键盘按键)开门
- Web3.0 Dapps - the road to the future financial world
- MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
猜你喜欢
mutillidae下载及安装
Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
Bubble Sort and Quick Sort
Haproxy搭建Web群集
How to wrap markdown - md file
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
今年七夕,「情蔬」比礼物更有爱
The most effective seven performance testing techniques of software testing techniques
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
随机推荐
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
UE4 第一人称角色模板 添加冲刺(加速)功能
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
多御安全浏览器新版下载 | 功能优秀性能出众
惨遭打脸:字节某部门竟有这么多测试员
大佬们,我注意到mysql cdc connector有参数scan.incremental.sna
The most effective seven performance testing techniques of software testing techniques
public static <T> List<T> asList(T... a) 原型是怎么回事?
测试薪资这么高?刚毕业就20K
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
How to find all fields with empty data in sql
Ali's local life's single-quarter revenue is 10.6 billion, Da Wenyu's revenue is 7.2 billion, and Cainiao's revenue is 12.1 billion
36-Jenkins-Job迁移
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
Initial solution of the structure
ffmpeg 枚举decoders, encoders 分析
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
Bubble Sort and Quick Sort
How to discover a valuable GameFi?