当前位置:网站首页>[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)
边栏推荐
- 概率论的学习和整理8: 几何分布和超几何分布
- How to find all fields with empty data in sql
- Fifteen. Actual combat - MySQL database building table character set and collation
- How to solve the three major problems of bank data collection, data supplementary recording and index management?
- UE4 opens door via interaction (keyboard key)
- iMedicalLIS监听程序(2)
- ffmpeg -sources分析
- [论文笔记] MapReduce: Simplified Data Processing on Large Clusters
- MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
- Burp installation and proxy settings
猜你喜欢

UE4 opens doors with overlapping events

事件解析树Drain3使用方法和解释
![[Software testing] unittest framework for automated testing](/img/80/caedd5cf6dd61c9d75475866613cac.png)
[Software testing] unittest framework for automated testing

UE4 通过与其它Actor互动开门

The test salary is so high?20K just graduated

将故事写成我们

Web3.0 Dapps - the road to the future financial world

UE4 opens door via interaction (keyboard key)

Dive into how it works together by simulating Vite

UE4 第一人称角色模板 添加蹲伏功能
随机推荐
Web3.0 Dapps - the road to the future financial world
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
Qixi Festival code confession
JeeSite新建报表
惨遭打脸:字节某部门竟有这么多测试员
Redis key basic commands
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
十五. 实战——mysql建库建表 字符集 和 排序规则
UE4 通过互动(键盘按键)开门
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
Hard power or soft power, which is more important to testers?
Solana NFT开发指南
Swing有几种常用的事件处理方式?如何监听事件?
MySql的索引学习和使用;(本人觉得足够详细)
如何解决复杂的分销分账问题?
[GYCTF2020]EasyThinking
public static
List asList(T... a) What is the prototype? markdown如何换行——md文件
Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段