当前位置:网站首页>1696D. Permutation graph thinking
1696D. Permutation graph thinking
2022-06-28 00:33:00 【Strezia】
link
thinking
The question
Give a sequence a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an by 1 ~ n 1~n 1~n The whole arrangement . i , j i,j i,j There is an undirected edge between if and only if a i , a j a_i,a_j ai,aj Exactly a i , . . . , a j a_i,...,a_j ai,...,aj The minimum and maximum values of these numbers ( A minimum and a maximum ). The length of each edge is 1, Ask from 1 1 1 To n n n What is the shortest distance . 1 ≤ n ≤ 2.5 × 1 0 5 1\leq n\leq2.5\times10^5 1≤n≤2.5×105.
Ideas
set up d i s ( x , y ) dis(x,y) dis(x,y) Express x x x and y y y The shortest circuit length between .
consider a i = n a_i = n ai=n The location of , And suppose i ≠ 1 , i ≠ n i \neq 1,i\neq n i=1,i=n , obvious , from 1 → n 1\to n 1→n , Must go through i i i spot . In the same way a j = 1 a_j=1 aj=1 The location of , Allied , Must go through j j j spot , Then there are d i s ( x , y ) = d i s ( 1 , i ) + 1 + d i s ( j , n ) dis(x,y)=dis(1,i) + 1 + dis(j, n) dis(x,y)=dis(1,i)+1+dis(j,n) It's assumed here that i < j i<j i<j, If j > i j>i j>i The same goes for . In the middle of the 1 1 1 That is to say d i s ( i , j ) dis(i,j) dis(i,j), because a i , a j a_i,a_j ai,aj Respectively n n n and 1 1 1, So obviously there are edges that can be reached in one step .
Next, continue the recursive processing d i s ( 1 , i ) dis(1,i) dis(1,i) and d i s ( j , n ) dis(j,n) dis(j,n) . Just find The minimum and maximum values of the interval And continue recursion .
Just preprocess the subscript corresponding to the minimum value of prefix and suffix . Time complexity O ( n ) O(n) O(n) .
Code
int n;
int a[maxn], id[maxn];
int l_min[maxn], l_max[maxn];// front i Minimum number / The subscript of the largest value
int r_min[maxn], r_max[maxn];// after n-i Minimum number / The subscript of the largest value
int dfs(int l, int r) {
if(l == r) return 0;
if(l != 1 && r != n) return 1;
int L, R;
if(l == 1) {
L = min(l_min[r], l_max[r]);
R = max(l_min[r], l_max[r]);
}
else {
L = min(r_min[l], r_max[l]);
R = max(r_min[l], r_max[l]);
}
if(l == L && r == R) return 1;
return dfs(l, L) + dfs(L, R) + dfs(R, r);
}
void solve() {
cin >> n;
l_min[0] = r_min[n+1] = INF;
l_max[0] = r_max[n+1] = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
l_min[i] = min(l_min[i-1], a[i]);
l_max[i] = max(l_max[i-1], a[i]);
id[a[i]] = i;
}
for(int i = n; i; i--) {
r_max[i] = max(r_max[i+1], a[i]);
r_min[i] = min(r_min[i+1], a[i]);
// cout << l_max[i] << endl;
}
for(int i = 1; i <= n; i++) {
l_min[i] = id[l_min[i]];
r_min[i] = id[r_min[i]];
l_max[i] = id[l_max[i]];
r_max[i] = id[r_max[i]];
// cout << l_min[i]<<endl;
}
cout << dfs(1, n) << endl;
}
边栏推荐
- flutter系列之:flutter中的变形金刚Transform
- MATLAB basic function length function
- Arduino UNO通过电容的直接检测实现简易触摸开关
- 炼金术(2): 为什么要用issue管理软件
- [读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
- 代码整洁之道--格式
- 【论文阅读|深读】SDNE:Structural Deep Network Embedding
- 每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!
- Modern programming language: rust
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
猜你喜欢

免费、好用、强大的开源笔记软件综合评测

Sell notes | brief introduction to video text pre training
![[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science

认识微信小程序项目的基本组成结构

Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
![[digital ic/fpga] detect the position of the last matching sequence](/img/67/a1b575aa9b63892ed585d39e615c58.png)
[digital ic/fpga] detect the position of the last matching sequence
![[untitled]](/img/e4/7c65c6823559b8501a1777cc4eb7ba.jpg)
[untitled]
![Count prime [enumeration - > space for time]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
Count prime [enumeration - > space for time]

炼金术(7): 何以解忧,唯有重构

NoSQL之Redis配置与优化
随机推荐
Recyclerview implements grouping effects in a variety of ways
Arduino UNO通过电容的直接检测实现简易触摸开关
zotero文献管理工具安装使用
炼金术(7): 何以解忧,唯有重构
Acwing第 57 场周赛【未完结】
现代编程语言:zig
吴恩达《机器学习》课程总结(13)_聚类
Logging log usage
Is the securities registration account safe? Is there any risk?
最新MySQL高级SQL语句大全
100 questions for an enterprise architect interview
用两个栈实现队列[两次先进后出便是先进先出]
[untitled]
Instructions for vivado FFT IP
Deployment and test of crtmp live video server
Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
炼金术(1): 识别项目开发中的ProtoType、Demo、MVP
炼金术(9): 简约而不简单,永不停歇的测试 -- always_run
C语言malloc函数的功能及用法
QStringList 的学习笔记