Good Bye 2019 B. Interesting Subarray

链接:

https://codeforces.com/contest/1270/problem/B

题意:

For an array a of integers let's denote its maximal element as max(a), and minimal as min(a). We will call an array a of k integers interesting if max(a)−min(a)≥k. For example, array [1,3,4,3] isn't interesting as max(a)−min(a)=4−1=3<4 while array [7,3,0,4,3] is as max(a)−min(a)=7−0=7≥5.

You are given an array a of n integers. Find some interesting nonempty subarray of a, or tell that it doesn't exist.

An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

思路:

相邻两个能成立即可。

代码:

#include
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;

int a[MAXN];
int n;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for (int i = 1;i <= n;i++)
            cin >> a[i];
        bool flag = false;
        int l, r;
        for (int i = 1;i < n;i++)
        {
            if (abs(a[i]-a[i+1]) >= 2)
            {
                flag = true;
                l = i, r = i+1;
                break;
            }
        }
        if (flag)
            cout << "YES\n" << l << ' ' << r << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

你可能感兴趣的:(Good Bye 2019 B. Interesting Subarray)