当前位置:网站首页>[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
2022-08-05 03:59:00 【ZhgDgE】
#846. 第二大数字和
题意:给定长度为 n ( 2 ≤ n ≤ 1 0 5 ) n(2\leq n\leq 10^5) n(2≤n≤105) 的排列,Ask all lengths at least 2 2 2 What is the sum of the next largest values of the subsections of .
题解:(数据结构) 代码源每日一题 Div1 第二大数字和
(O(n)做法) 代码源每日一题 Div1 第二大数字和
思路:For the subsection problem,We usually start recursively from the subscript.但是 The question of the second largest value of the subparagraph of this question starts from the perspective of the value range .
First the problem turns into :Find each number x x x The first on the left and right is greater than x x x and the second is greater than x x x 的数的下标,详见题解.
We have an ordered data structure,Then we enumerate each number from largest to smallest,Then set the current number a i a_i ai 的下标 i i i 添加进去.Calculate the first and second largest numbers before adding,The first number to the right of this subscript in the data structure,即为 a i a_i ai The first one on the right in the sequence is greater than a i a_i ai 的下标,The second largest number is the same.用 s e t set set 实现即可.
O ( n ) O(n) O(n) The approach is to build first 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 的链表,Then enumerate the subscripts of each number from small to large,After enumerating a certain number, delete the subscript in the linked list.The subscript of this number on the left and right sides of the linked list is the subscript of the first large number,The second largest number is the same.
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
思路:循环论证.First if the sequence exists 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 时,为必胜态(It can be operated once to convert 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 时,为必输态(The operation must be converted once 2. ).
- 当 0 < c o u n t ( 1 ) ≤ n 2 0<count(1)\leq \frac n 2 0<count(1)≤2n 时,为必胜态(It can be operated once to convert 3. ).
类似于这样,If the number of occurrences of the minimum value > n 2 >\frac n 2 >2n ,It is mandatory,Because the number of times the new minimum value must be after the operation ( 0 , n 2 ] (0,\frac n 2] (0,2n] 内的,That is to win;On the contrary, it is a winning state,Because it is possible to make the minimum heap number from by choosing a non-minimum heap ( 0 , n 2 ] (0,\frac n 2] (0,2n] 变为 ( n 2 , n ] (\frac n 2, n] (2n,n] ,must lose.
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 nodes with a height of j j j 的方案数,Enumerates the number of nodes on the left 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] ,I thought of it after reading the solution j j j 最大为 20 20 20 .时间复杂度 O ( 20 × n 2 ) O(20\times n^2) O(20×n2)
边栏推荐
- The most comprehensive exam questions for software testing engineers in 2022
- Queue Topic: Recent Requests
- Kubernetes 网络入门
- bytebuffer 使用demo
- 结构体初解
- [BSidesCF 2019]Kookie
- 2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
- 调用阿里云oss和sms服务
- 如何解决复杂的分销分账问题?
- Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
猜你喜欢
随机推荐
2022软件测试工程师最全面试题
ffmpeg -sources分析
新人如何入门和学习软件测试?
达梦8数据库导出导入
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
工业级远距离无线传输装置的功能有哪些?
XMjs cross-domain problem solving
不看后悔,appium自动化环境完美搭建
rpc-remote procedure call demo
[MRCTF2020]PYWebsite
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
银行数据采集,数据补录与指标管理3大问题如何解决?
2022 Hangzhou Electric Multi-School 1st Game
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
十五. 实战——mysql建库建表 字符集 和 排序规则
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
程序开发的一些常规套路(一)
ffmpeg enumeration decoders, encoders analysis
Walter talked little knowledge | "remote passthrough" that something
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层









