1760D: Challenging Valleys

If the curve has more than two local minima, then it must also have a local maximum, and hence it contains a peak. Valleys only, around here.


_72
#if NOT_LINUX
_72
#include "macos_includes.h"
_72
#else
_72
#include <bits/stdc++.h>
_72
#endif
_72
_72
#if LOCAL
_72
#include "debug.h"
_72
#include "gxlib.h"
_72
#else
_72
#define dbg(...) 'x'
_72
#endif
_72
_72
using namespace std;
_72
using ll = long long;
_72
const char nl = '\n';
_72
const int MAX_INT = std::numeric_limits<int>::max();
_72
const ll MAX_LL = std::numeric_limits<ll>::max();
_72
_72
void solve() {
_72
int n;
_72
cin >> n;
_72
vector<ll> a(n), adjR(n), adjL(n);
_72
for (auto& i : a) {
_72
cin >> i;
_72
}
_72
_72
a.erase(unique(a.begin(), a.end()), a.end());
_72
_72
int minima = 0;
_72
_72
int m = a.size();
_72
_72
for (int i = 0; i < m; ++i) {
_72
if (i == 0) {
_72
if (a[i] < a[i+1])
_72
minima++;
_72
_72
continue;
_72
} else if (i == m-1) {
_72
if (a[i] < a[i-1])
_72
minima++;
_72
_72
continue;
_72
}
_72
_72
if (a[i-1] > a[i] && a[i] < a[i+1])
_72
minima++;
_72
_72
if (minima >= 2) break;
_72
}
_72
_72
if (minima < 2) {
_72
cout << "YES\n";
_72
} else {
_72
cout << "NO\n";
_72
}
_72
}
_72
_72
_72
int main() {
_72
ios::sync_with_stdio(false);
_72
cin.tie(nullptr);
_72
_72
int t;
_72
cin >> t;
_72
for (int tc = 1; tc <= t; ++tc) {
_72
//cout << "Case " << tc << ":\n";
_72
solve();
_72
}
_72
return 0;
_72
}

If you found this solution helpful, consider leaving a star!