当前位置:网站首页>【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)
边栏推荐
- 多御安全浏览器新版下载 | 功能优秀性能出众
- 36-Jenkins-Job迁移
- 10 years of testing experience, worthless in the face of the biological age of 35
- 用Unity发布APP到Hololens2无坑教程
- What is the difference between SAP ERP and ORACLE ERP?
- token、jwt、oauth2、session解析
- Burp installation and proxy settings
- YYGH-13-Customer Service Center
- Never put off till tomorrow what you can put - house lease management system based on the SSM
- 阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
猜你喜欢

Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..

测试薪资这么高?刚毕业就20K

The test salary is so high?20K just graduated

MySql的索引学习和使用;(本人觉得足够详细)

YYGH-13-客服中心

UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)

Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full

2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto

Open-Falcon of operation and maintenance monitoring system

Defect detection (image processing part)
随机推荐
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
token, jwt, oauth2, session parsing
Web3.0 Dapps - the road to the future financial world
MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
The most effective seven performance testing techniques of software testing techniques
Shell script: for loop and the while loop
银行数据采集,数据补录与指标管理3大问题如何解决?
Fifteen. Actual combat - MySQL database building table character set and collation
public static <T> List<T> asList(T... a) 原型是怎么回事?
用CH341A烧录外挂Flash (W25Q16JV)
UE4 通过互动(键盘按键)开门
GC Gaode coordinate and Baidu coordinate conversion
Why is the pca component not associated
Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
Use Unity to publish APP to Hololens2 without pit tutorial
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
After the large pixel panorama is completed, what are the promotion methods?
ffmpeg 枚举decoders, encoders 分析
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)