当前位置:网站首页>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;
}
边栏推荐
- Recyclerview implements grouping effects in a variety of ways
- MySQL enterprise parameter tuning practice sharing
- Modern programming language: rust
- The development of the Internet provides new solutions for industrial transformation
- 2022 PMP project management examination agile knowledge points (3)
- Feign通过自定义注解实现路径的转义
- Is it safe for Huatai Securities to open an account online?
- 证券注册账户安全吗,会有风险吗?
- Customize MySQL connection pool
- GFS 分布式文件系统概述与部署
猜你喜欢

Sell notes | brief introduction to video text pre training

【无标题】

技术的极限(11): 有趣的编程
![Software engineering job design (1): [personal project] implements a log view page](/img/95/0c3f0dde16d220ddecb5758a4c31e7.png)
Software engineering job design (1): [personal project] implements a log view page

智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维

Sword finger offer 61 Shunzi in playing cards

Technical debt wall: a way to make technical debt visible and negotiable

MySQL分表查询之Merge存储引擎实现
![[untitled]](/img/e4/7c65c6823559b8501a1777cc4eb7ba.jpg)
[untitled]

云厂商为什么都在冲这个KPI?
随机推荐
用两个栈实现队列[两次先进后出便是先进先出]
Quickly master grep commands and regular expressions
投资场内ETF基金是靠谱吗,场内ETF基金安全吗
股市小白在网上股票开户安全吗?
华泰证券在网上开户安全吗?
Modern programming languages: zig
内网IP和公网IP的区别及作用
Local visualization tool connects to redis of Alibaba cloud CentOS server
Matlab基本函数 length函数
夏日的晚会
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
Translation (4): matching rules for automatic text completion
Alchemy (7): how to solve problems? Only reconstruction
Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!
NDSS 2022 received list
RecyclerView实现分组效果,多种实现方式
供应链高效管理供应商
快速掌握grep命令及正则表达式
[digital ic/fpga] detect the position of the last matching sequence
TIME_WAIT过多的解决办法