mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 13:56:26 +00:00
136. 邻值查找
https://www.acwing.com/problem/content/submission/code_detail/8040990/
This commit is contained in:
parent
e2d63a2999
commit
12dbf98263
27
AcWing/136/136.cpp
Normal file
27
AcWing/136/136.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n, x;
|
||||
set<pair<int, int>> s;
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
s.insert({make_pair(INT_MIN, 0), make_pair(INT_MAX, 0)});
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> x;
|
||||
if (i > 1) {
|
||||
auto it1 = s.upper_bound(make_pair(x, 0));
|
||||
auto it2 = it1--;
|
||||
long long x1 = 0ll + x - it1->first;
|
||||
long long x2 = 0ll + it2->first - x;
|
||||
if (x1 <= x2) {
|
||||
cout << x1 << ' ' << it1->second << endl;
|
||||
} else {
|
||||
cout << x2 << ' ' << it2->second << endl;
|
||||
}
|
||||
}
|
||||
s.insert(make_pair(x, i));
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user