Cod sursa(job #2059378)

Utilizator mihai.alphamihai craciun mihai.alpha Data 6 noiembrie 2017 22:08:03
Problema Secventa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

#define BUF 1 << 17
char buf[BUF];
int pos = BUF;

inline char next()  {
    if(pos == BUF)
        fread(buf, 1, BUF, stdin), pos = 0;
    return buf[pos++];
}

inline int read()  {
    int x = 0, semn = 1;
    char ch = next();
    while(!isdigit(ch) && ch != '-')
        ch = next();
    if(ch == '-')
        ch = next(), semn = -1;
    while(isdigit(ch))
        x = x * 10 + ch - '0', ch = next();
    return x * semn;
}

const int MAXN = 5e5 + 5;

int n, k;
int v[MAXN];

deque <int> q;

int main()  {
    freopen("secventa.in", "r", stdin);
    freopen("secventa.out", "w", stdout);
    n = read(), k = read();
    for(int i = 1;i <= n;i++)
        v[i] = read();
    for(int i = 1;i < k;i++)  {
        while(q.size() && v[q.back()] > v[i])
            q.pop_back();
        q.push_back(i);
    }
    int mini = q.front();
    int st, dr;
    st = 1, dr = k;
    for(int i = k;i <= n;i++)  {
        while(q.size() && i - q.front() + 1 > k)
            q.pop_front();
        while(q.size() && v[q.back()] > v[i])
            q.pop_back();
        q.push_back(i);
        if(v[mini] < v[q.front()])
            mini = q.front(), st = i - k + 1, dr = i;
    }
    cout << st << " " << dr << " " << v[mini];
    return 0;
}