# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair <int, int>
using namespace std;
const int N = 3e5 + 5;
int t,n,a[N],pr[N],sf[N],cur,le,ri;
int tree[4*N];
void build(int node, int le ,int ri) {
if (le == ri) {
tree[node] = a[le];
return ;
}
int mid = (le + ri) / 2;
build(2*node,le,mid);
build(2*node + 1, mid + 1, ri);
tree[node] = min(tree[2*node],tree[2*node + 1]);
}
int getans(int node, int le, int ri, int start,int end) {
if (le > end || ri < start) {
return 2e9;
}
if (le >= start && ri <= end) {
return tree[node];
}
int mid = (le + ri) / 2;
return min(getans(2*node,le,mid,start,end), getans(2*node + 1, mid + 1, ri, start,end));
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>a[i];
}
for (int i = 1; i <= n; i++) {
cur = i - 1;
while (a[cur] <= a[i] && cur) {
cur = pr[cur];
}
pr[i] = cur;
}
for (int i = n; i >= 1; i--) {
cur = i +1;
while (a[cur] <= a[i] && cur <= n) {
cur = sf[cur];
}
sf[i] = cur;
}
build(1,1,n);
int ans = 2e9;
for (int i = 1; i < n ;i++) {
le = pr[i] + 1;
ri = sf[i] - 1;
// ans = min(ans, abs(a[i] - a[i - 1]));
ans = min(ans, abs(a[i] - a[i + 1]));
}
cout<<ans<<endl;
}
Compilation message
pismo.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
30 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
18 ms |
2300 KB |
Output is correct |
6 |
Correct |
13 ms |
2332 KB |
Output is correct |
7 |
Correct |
17 ms |
2356 KB |
Output is correct |