当前位置:网站首页>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;
}
边栏推荐
- MATLB|改进的前推回代法求解低压配电网潮流
- Internship: business process introduction
- 现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)
- Validaterequest= "false" is a "suggestion collection" for what
- Modern programming languages: zig
- How to use raspberry pie (and all kinds of pies)
- Instructions for vivado FFT IP
- 剑指 Offer 65. 不用加减乘除做加法
- 自定义MySQL连接池
- 本地可视化工具连接阿里云centOS服务器的redis
猜你喜欢

What is promise

MySQL企业级参数调优实践分享
![Using two stacks to implement queues [two first in first out is first in first out]](/img/de/07297816f1a44d41389bb45d012c80.png)
Using two stacks to implement queues [two first in first out is first in first out]
![[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

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

flutter系列之:flutter中的变形金刚Transform

什么是cookie,以及v-htm的安全性隐患

炼金术(1): 识别项目开发中的ProtoType、Demo、MVP

Sell notes | brief introduction to video text pre training

免费、好用、强大的开源笔记软件综合评测
随机推荐
MYSQL的下载与配置安装
What are the ways to combine the points system with marketing activities
Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
[VIM] tutorial, common commands, efficient use of vim editor
NoSQL之Redis配置与优化
认识微信小程序项目的基本组成结构
The Internet industry has derived new technologies, new models and new types of industries
The limits of Technology (11): interesting programming
用两个栈实现队列[两次先进后出便是先进先出]
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
Mysql database tourism management system_ Jsp+mysql tourism management system based on SSM [easy to understand]
Alchemy (3): how to do a good job in interfacing a business process
Eliminate gaps around El image images
GFS 分布式文件系统概述与部署
Introduction to data warehouse
HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)
[paper reading | deep reading] sdne:structural deep network embedding
MATLB|改进的前推回代法求解低压配电网潮流
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
剑指 Offer 65. 不用加减乘除做加法