当前位置:网站首页>[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 .
边栏推荐
- 动态内存与智能指针
- Understand various paths
- [hcip] experiment of republishing and routing strategy
- 数据平台数据接入实践
- ELS stop at all
- After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
- Code generator
- Openpyxl merge cells
- 【流放之路-第七章】
- Use of resttemplate and Eureka
猜你喜欢

动态内存与智能指针

Use of resttemplate and Eureka

With the explosive growth of digital identity in 2022, global organizations are facing greater network security

10 major network security incidents in the past 10 years

Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years

明日无限计划,2022某公司元宇宙产品发布会活动概念策划方案

Openpyxl cell center

T-sne dimensionality reduction

采用QT进行OpenGL开发(二)绘制立方体

【GoLang】同步锁 Mutex
随机推荐
【GoLang】同步锁 Mutex
Nacos installation guide on win system
Sigma-DSP-OUTPUT
规划数学期末考试模拟二
Cloud native application comprehensive exercise
BOM系列之定时器
Lombook User Guide
规划数学期末模拟考试一
Embedded sharing collection 23
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
J9 number theory: what factors determine the value of NFT?
Network security litigation risk: four issues that chief information security officers are most concerned about
What is the ISO assessment? How to do the waiting insurance scheme
LeetCode 113:路径总和 II
剑指offer专项突击版第13天
The new generation of public chain attacks the "Impossible Triangle"
Pinduoduo can use many API interfaces
【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
The brutal rule of blackmail software continues, and attacks increase by 105%
【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】