当前位置:网站首页>[7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
[7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
2022-07-29 01:50:00 【ZhgDgE】
#607. Square count
The question : From sequence M M M Choose from the numbers in order N ( n ≤ 1 0 6 ) N(n\leq 10^6) N(n≤106) A different number , Make this N N N The dictionary order of the number is the smallest .
Answer key :( Multiple traversal ) Code source daily question Div1 Square count
Ideas : If the point is right ( i , j ) (i,j) (i,j) Satisfy a i 2 + a j a_i ^2+a_j ai2+aj Is the perfect square , that a j = ( x + a i ) × ( x − a i ) a_j=(x+a_i)\times (x-a_i) aj=(x+ai)×(x−ai) . We can enumerate a j a_j aj Factor pair of , Then you can get the one that meets the conditions a i a_i ai How much is the .
Here we need to use the similar optimization in the previous article . The method of sifting the factors of a number :
- Violent root enumeration , Time complexity O ( 0 ∼ n ) O(0\sim \sqrt n) O(0∼n)
- Ehrlich sieve optimization , Enumerate the multiples of each number , Then the number must be a factor of multiple , You can get a factor pair . Time complexity O ( n × log n ∼ log n ) O(n\times \log n\sim \log n) O(n×logn∼logn)
This problem uses the second optimization . However, if the code is implemented, there is no need to store factor pairs .
rep(i, 1, 1000000){
for(int j = i; j <= 1000000; j += i){
if(j / i - i < 0) continue;
// (i, j / i) That is, a pair of factor pairs
}
}
AC Code :http://oj.daimayuan.top/submission/301674
#608. Dictionary order is the smallest
The question : From sequence M M M Choose from the numbers in order N N N A different number , Make this N N N The dictionary order of the number is the smallest . ( n , m ≤ 1 0 6 , 1 ≤ a i ≤ N ) (n,m\leq 10^6,1\leq a_i \leq N) (n,m≤106,1≤ai≤N) , Data assurance [ 1 , N ] [1,N] [1,N] Each number in the range occurs at least once .
Answer key :( Monotonic stack ) Code source daily question Div1 Dictionary order is the smallest
Ideas : And the last one div2 The greedy thinking of the question is very similar . First of all, we should approach the minimum lexicographic order , That is to say [ 1 , N ] [1,N] [1,N] Original arrangement . So we maintain a stack , Sweep backward , Pop up all numbers larger than the new ones at the end of the stack , Put the new number in . This can greedily move closer to the smallest dictionary order . But our decision may have problems , If there is no element equal to the number to pop up , There will be no solution . therefore , Preprocess whether there is a number after each number , If there are several, the current number can pop up ; Otherwise, it cannot pop up , Because it will cause no solution .
AC Code :http://oj.daimayuan.top/submission/292414
#614. “Z” Type matrix
The question : Ask how many submatrixes of a matrix satisfy the matrix shape z .
Answer key :( offline / Tree array ) Code source daily question Div1“Z” Type matrix
Ideas : The endpoint of the enumeration matrix is O ( n 4 ) O(n^4) O(n4) Of . How to optimize ? We solve on the diagonal . For each diagonal , Every point above has a left line 、 Right line , We sweep diagonals from top right to bottom left , In the process of scanning, maintain according to the length of the left line , Save to the tree array . Because every left-hand line has a life cycle , We mark the end time of each left line . For the right line , What can match the current right-hand line must be the left-hand line that is still alive with a length less than or equal to the right-hand line , Just query the tree array .
边栏推荐
- Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
- HCIA配置实例(eNSP)
- Embedded sharing collection 23
- ELS square movement
- Making high-precision map based on autoware (V)
- StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
- numpy.where() 用法和np.argsort()的用法
- New upgrade: get Taobao product details "advanced version" API
- numpy. Where() usage and np.argsort() usage
- Nacos installation guide on win system
猜你喜欢

JVM learning minutes

LeetCode 113:路径总和 II

Platofarm community ecological gospel, users can get premium income with elephant swap
![About df['a column name'] [serial number]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
About df['a column name'] [serial number]

Anaconda environment installation problem

Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures

Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value

T-sne dimensionality reduction
![[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area](/img/07/565ca7145bcbef2d467b3c860b7487.png)
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area

How many of the top ten test tools in 2022 do you master
随机推荐
Analysis of Multi Chain use cases on moonbeam -- review of Derek's speech in Polkadot decoded 2022
New 1688 API access instructions
Use of resttemplate and Eureka
MPEG音频编码三十年
How to choose professional, safe and high-performance remote control software
It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
Slow storage scheme
mysql的执行顺序
In depth analysis of C language memory alignment
internship:用于类型判断的工具类编写
ELS square movement
JVM learning minutes
How many of the top ten test tools in 2022 do you master
新生代公链再攻「不可能三角」
HCIA配置实例(eNSP)
Ruiji takeout project actual battle day01
秘术冬潮烙技能搭配
【搜索】—— DFS之剪枝与优化
New upgrade: get Taobao product details "advanced version" API
Reinforcement learning (I): Q-learning, with source code interpretation