当前位置:网站首页>[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)
边栏推荐
- How do newcomers get started and learn software testing?
- 新人如何入门和学习软件测试?
- flink读取mongodb数据源
- Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
- MySql的索引学习和使用;(本人觉得足够详细)
- MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
- 2022软件测试工程师最全面试题
- Qixi Festival code confession
- 程序开发的一些常规套路(一)
- 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
猜你喜欢
YYGH-13-客服中心
token, jwt, oauth2, session parsing
bytebuffer 使用demo
36-Jenkins-Job Migration
Dive into how it works together by simulating Vite
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
UE4 通过重叠事件开启门
This year's Qixi Festival, "love vegetables" are more loving than gifts
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
随机推荐
[Paper Notes] MapReduce: Simplified Data Processing on Large Clusters
GC Gaode coordinate and Baidu coordinate conversion
35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
Use Unity to publish APP to Hololens2 without pit tutorial
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
[CISCN2019 华东南赛区]Web11
[极客大挑战 2019]FinalSQL
Fifteen. Actual combat - MySQL database building table character set and collation
[SWPU2019]Web1
Redis key basic commands
结构体初解
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
新人如何入门和学习软件测试?
十五. 实战——mysql建库建表 字符集 和 排序规则
You may use special comments to disable some warnings. 报错解决的三种方式
Spark Basics [Introduction, Getting Started with WordCount Cases]
【8.3】代码源 - 【喵 ~ 喵 ~ 喵~】【树】【与】
Dive into how it works together by simulating Vite
UE4 通过互动(键盘按键)开门