当前位置:网站首页>[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)
边栏推荐
- 十五. 实战——mysql建库建表 字符集 和 排序规则
- ffmpeg 像素格式基础知识
- [BJDCTF2020]EasySearch
- Summary of common methods of arrays
- [MRCTF2020]PYWebsite
- 七夕节代码表白
- [极客大挑战 2019]FinalSQL
- JeeSite新建报表
- MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
- Spark Basics [Introduction, Getting Started with WordCount Cases]
猜你喜欢

七夕节代码表白

shell脚本:for循环与while循环
![Spark Basics [Introduction, Getting Started with WordCount Cases]](/img/90/ebe887db0f8c36895691dea05f62cf.png)
Spark Basics [Introduction, Getting Started with WordCount Cases]

日志导致线程Block的这些坑,你不得不防

概率论的学习和整理8: 几何分布和超几何分布

Spark基础【介绍、入门WordCount案例】

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

Acid (ACID) Base (BASE) Principles for Database Design

token, jwt, oauth2, session parsing

public static
List asList(T... a) What is the prototype?
随机推荐
2022软件测试工程师最全面试题
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迁移
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
【背包九讲——01背包问题】
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
UE4 opens door via interaction (keyboard key)
Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
Web3.0 Dapps - the road to the future financial world
You may use special comments to disable some warnings. 报错解决的三种方式
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
How do newcomers get started and learn software testing?
达梦8数据库导出导入
rpc-remote procedure call demo
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
How to discover a valuable GameFi?
事件解析树Drain3使用方法和解释
Slapped in the face: there are so many testers in a certain department of byte
bytebuffer 使用demo
新人如何入门和学习软件测试?