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

Posted kanoon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}

 

以上是关于Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27