当前位置:网站首页>AcWing 136. Adjacent value lookup
AcWing 136. Adjacent value lookup
2022-07-19 13:47:00 【hys__ handsome】
Ideas
It's easy to think of sequence sorting , The difference between at least one of the left and right elements of the current element and the current element is the smallest in the whole sequence .
Then the topic needs to be found m i n 1 ≤ j < i ∣ A i − A j ∣ min_{1≤j<i}|A_i−A_j| min1≤j<i∣Ai−Aj∣ The smallest of all A j A_j Aj , It will be A A A After sorting, adjacent subscripts may be larger than the current subscript .
To solve this problem, we need a two-way linked list , from A A A The last element of the sequence begins to calculate , Delete the node directly after calculation , In the next calculation, there will be no adjacent points larger than the current subscript .
- a a a The array represents the data field of the linked list , The node order of the linked list is a a a After the array is in ascending order .
- p p p Array quick get A A A Sequence number x x x The position of the number in the linked list .
- l l l And r r r The array represents the left and right node indexes of the linked list nodes .
- Then the left and right ends of the linked list have boundary problems , Just put two sentinels here .
#include<iostream>
#include<algorithm>
#include<cmath>
#define x first
#define y second
using namespace std;
typedef long long LL;
const int N = 100010;
pair<LL,int> a[N], ans[N];
int p[N],l[N],r[N];
int main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0);
int n; cin>>n;
for(int i = 1; i <= n; i++){
cin>>a[i].x;
a[i].y = i;
}
sort(a+1, a+1+n);
a[0].x = a[n+1].x = -4e9; // Put two sentinels to prevent border problems
for(int i = 1; i <= n; i++){
l[i] = i-1, r[i] = i+1;
p[a[i].y] = i; // Mapping position
}
for(int i = n; i > 1; i--){
int j = p[i], left = l[j], right = r[j];
LL lv = abs(a[j].x - a[left].x);
LL rv = abs(a[j].x - a[right].x);
if(lv <= rv) ans[i] = {
lv, a[left].y};
else ans[i] = {
rv, a[right].y};
r[left] = right;
l[right] = left;
}
for(int i = 2; i <= n; i++) cout<< ans[i].x << ' ' << ans[i].y << '\n';
return 0;
}
边栏推荐
- [pumpkin Book ml] (task2) mathematical derivation of linear model (least squares estimation, generalized Rayleigh quotient, maximum likelihood estimation, etc.)
- 【码蹄集新手村 600 题】运算符 / 在不同的运算顺序中的类型转换
- [code hoof set novice village 600 question] calculate the number of digits of an integer
- Qt之使用QLisView实现QQ登录历史列表
- Start from here by counting the information of the broadcast room
- Elastic load balancing automatically distributes the access traffic to multiple cloud servers, expands the external service capacity of the application system, and improves the security of the applica
- S32K148_ Can drive (bare metal development)
- Module 7 (Architecture Design of King glory mall)
- Android开发大厂面试,做好这3个准备,规划好自己
- "Podcast with relish" 392 original soup, original food: midsummer night, mashed horse, kebab, and pull board
猜你喜欢

NO.4位、字节、信息存储

Onvif protocol related: 2.1.2 get screenshot URL in none mode

【7.15】代码源 -【整齐的数组2】【三进制循环】【树上逆序对】【蜗蜗的数列】

NO.5整数的表示与运算
![[code hoof set novice village question 600] formatted input and output, using 0 to replace the completed space](/img/15/5a13501272165e6488e38a98e3c69c.png)
[code hoof set novice village question 600] formatted input and output, using 0 to replace the completed space

onvif协议相关:4.1.4 WS-Username token方式获取流地址

「津津乐道播客」#392 原汤话原食:仲夏夜,马砂、肉串儿、趿拉板儿
![[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism](/img/50/07dd47d1bc46681e19d133b2e219c4.png)
[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism

Responsive Zhimeng template logistics and freight service website

统计直播间的榜一信息,从这里起步
随机推荐
onvif协议相关:4.1.3 WS-Username token方式获取截图url
[micro Service ~ advanced] configuration center practice
ArrayList underlying analysis
uniapp 高德地图定位功能
[code hoof set novice village 600 question] calculate the number of digits of an integer
Some common operation commands of the command line and solutions to common errors
Helloword and led: a quick start to Hongmeng device development -- Huawei cloud 14 day Hongmeng device development practical learning notes Chapter 2
STL string replication comparison
【7.8】Educational Codeforces Round 131 (Rated for Div. 2)
Android开发大厂面试,做好这3个准备,规划好自己
"Podcast" \ # 392 original Tang Lang original food: MIDSUMMER night, ma Sha, kebab, Kebab
How to earn money in low-level cities
onvif协议相关:4.1.1 WS-Username token方式获取WSUsernameTokenBean
2. Sum of three numbers
onvif协议相关:常用类说明
[postgraduate entrance examination vocabulary training camp] day 6 - eventually, state, create, productivity, stimulate
[从零开始学习FPGA编程-53]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Xilinx)
Introduction:Multiple DataFrames
【码蹄集新手村 600 题】如何使整数逆序
NO.6浮点数的表示与运算