当前位置:网站首页>【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
2022-07-29 00:53:00 【ZhgDgE】
#607. 平方计数
题意:从序列 M M M 个数中顺序选出 N ( n ≤ 1 0 6 ) N(n\leq 10^6) N(n≤106) 个不同的数, 使得这 N N N 个数的字典序最小。
思路:如果点对 ( i , j ) (i,j) (i,j) 满足 a i 2 + a j a_i ^2+a_j ai2+aj 为完全平方数,那么 a j = ( x + a i ) × ( x − a i ) a_j=(x+a_i)\times (x-a_i) aj=(x+ai)×(x−ai) 。我们可以枚举 a j a_j aj 的因子对,然后就能得到满足条件的 a i a_i ai 是多少。
这里要用到上一个文章里类似的优化。筛一个数的因子的方法:
- 暴力根号枚举,时间复杂度 O ( 0 ∼ n ) O(0\sim \sqrt n) O(0∼n)
- 埃氏筛优化,枚举每个数的倍数,那么该数一定是倍数的因子,就能得到一个因子对。时间复杂度 O ( n × log n ∼ log n ) O(n\times \log n\sim \log n) O(n×logn∼logn)
这道题用的是第二个优化。不过代码实现的话没有必要储存起来因子对。
rep(i, 1, 1000000){
for(int j = i; j <= 1000000; j += i){
if(j / i - i < 0) continue;
// (i, j / i) 即为一对因子对
}
}
AC代码:http://oj.daimayuan.top/submission/301674
#608. 字典序最小
题意:从序列 M M M 个数中顺序选出 N N N 个不同的数, 使得这 N N N 个数的字典序最小。 ( 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) ,数据保证 [ 1 , N ] [1,N] [1,N] 范围内每个数至少出现一次。
思路:和上一场的 div2 的题的贪心思路很像。首先我们要向最小字典序去靠近,也就是 [ 1 , N ] [1,N] [1,N] 原排列。于是我们维护一个栈,向后扫,把栈尾部所有比新来的数大的数弹出来,新数放进去。这样能贪心的向最小字典序靠拢。但是我们的决策可能出现问题,如果后面没有和要弹出的数相等的元素,就会无解。因此,预处理每个数后面是否还有数,如有数则当前数可弹出;否则不可弹出,因为会造成无解。
AC代码:http://oj.daimayuan.top/submission/292414
#614. “Z”型矩阵
题意:问一个矩阵有多少个子矩阵满足矩阵形状为 z 。
题解:(离线/树状数组)代码源每日一题 Div1“Z”型矩阵
思路:枚举矩阵端点是 O ( n 4 ) O(n^4) O(n4) 的。如何优化?我们在对角线上求解。对于每个对角线,上面的每一个点都有左向线、右向线,我们从右上向左下扫对角线,扫的过程中按照左向线的长度维护,存到树状数组里。因为每个左向线都有一个生存周期,我们标记每个左向线的结束时间。对于右向线,能和当前右向线匹配的一定是长度小于等于右向线的仍然存活的左向线,查询树状数组即可。
边栏推荐
- What are source code, inverse code and complement code
- 【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
- Analyze OP based on autoware_ global_ Planner global path planning module re planning
- J9 number theory: what factors determine the value of NFT?
- [hcip] experiment of republishing and routing strategy
- 2022年最火的十大测试工具,你掌握了几个
- 【搜索】—— 迭代加深/双向DFS/IDA*
- SQL injection of DVWA
- Django uses pymysql module to connect mysql database
- Reinforcement learning (III): dqn, nature dqn, double dqn, with source code interpretation
猜你喜欢

Autoware reports an error: can't generate global path for start solution

【Unity项目实践】合成大西瓜
![[unity project practice] synthetic watermelon](/img/60/20d4ef6f4ad99a9bdb7dc2b4dba23b.png)
[unity project practice] synthetic watermelon
![[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"

MySQL execution order

Where will Jinan win in hosting the first computing power conference?

【golang】使用select {}

围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育

SiC Power Semiconductor Industry Summit Forum successfully held

CSDN modify column name
随机推荐
【Web技术】1395- Esbuild Bundler HMR
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.
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
The solution to keep the height of the container unchanged during the scaling process of the uniapp movable view table
Openpyxl merge cells
SQL injection of DVWA
Embedded sharing collection 23
Lombook User Guide
ELS stop at all
What is the ISO assessment? How to do the waiting insurance scheme
Cloud native application comprehensive exercise
2022年最火的十大测试工具,你掌握了几个
ELS new box falls
Canal real-time parsing MySQL binlog data practice
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
ValueError: Colors must be aRGB hex values
J9 number theory: what factors determine the value of NFT?
Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device
els 新的方块落下