Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices

题目链接:https://codeforces.com/contest/1380/problem/A

题意

给出一个大小为 $n$ 的排列,找出是否有三个元素满足 $p_i < p_j and p_j > p_k$ 。

题解

如果排列为增序或降序则无解,否则一定存在三个相邻的元素满足 $p_i < p_{i+1} and p_{i+1} > p_{i+2}$ 。

证明

若不存在,则 $p_i ge p_{i+1} or p_{i+1} le p_{i+2}$,即排列为增序或降序。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    int a[n] = {};
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 1; i + 1 < n; i++) {
        if (a[i - 1] < a[i] and a[i] > a[i + 1]) {
            cout << "YES" << "
";
            cout << i << ' ' << i + 1 << ' ' << i + 2 << "
";
            return;
        }
    }
    cout << "NO" << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}
原文地址:https://www.cnblogs.com/Kanoon/p/13296606.html