105952024-04-06 12:24:44MagyarKendeSZLGSíkság (55)cpp17Elfogadva 55/5514ms9856 KiB
// O(N)
#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define size(v) (int)v.size()

using namespace std;
using ll = long long;

int main() {
    cin.tie(0), ios::sync_with_stdio(0);

    int N;
    cin >> N;
    vector<int> v(N);
    for (int& x : v) cin >> x;

    int min_height = v[0], max_height = v[0], min_height_pos = 0, max_height_pos = 0,
        result_i = 0, result_len = 1, l = 0, r = 1;
    
    while (r < N) {

        if (abs(v[r] - v[r - 1]) > 1) {
            l = r;
            min_height = max_height = v[r];
            min_height_pos = max_height_pos = r;
            r++;
            continue;
        }

        bool smoller = v[r - 1] > v[r] && min_height > v[r],
              bigger = v[r - 1] < v[r] && max_height < v[r];

        if (min_height != max_height && (bigger || smoller)) {

            if (bigger) {
                l = min_height_pos + 1;
                min_height = max_height;
                min_height_pos = max_height_pos;
                max_height = v[r];
                max_height_pos = r;
            } else {
                l = max_height_pos + 1;
                max_height = min_height;
                max_height_pos = min_height_pos;
                min_height = v[r];
                min_height_pos = r;
            }
        } else {
            if (min_height >= v[r]) {
                min_height = v[r];
                min_height_pos = r;
            }
            if (max_height <= v[r]) {
                max_height = v[r];
                max_height_pos = r;
            }
            if (result_len < r - l + 1) {
                result_len = r - l + 1;
                result_i = l;
            }
        }
        r++;
    }

    cout << result_len << " " << result_i + 1;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base55/55
1Elfogadva0/03ms1828 KiB
2Elfogadva0/04ms2088 KiB
3Elfogadva2/23ms2176 KiB
4Elfogadva2/23ms2392 KiB
5Elfogadva2/23ms2612 KiB
6Elfogadva2/23ms2976 KiB
7Elfogadva3/33ms3092 KiB
8Elfogadva2/23ms3484 KiB
9Elfogadva3/33ms3588 KiB
10Elfogadva3/34ms3632 KiB
11Elfogadva3/34ms3968 KiB
12Elfogadva3/34ms4056 KiB
13Elfogadva3/314ms5204 KiB
14Elfogadva3/39ms5380 KiB
15Elfogadva3/312ms5936 KiB
16Elfogadva3/313ms6412 KiB
17Elfogadva3/314ms7248 KiB
18Elfogadva3/314ms7708 KiB
19Elfogadva3/314ms8304 KiB
20Elfogadva3/314ms9008 KiB
21Elfogadva3/38ms9064 KiB
22Elfogadva3/312ms9856 KiB