Cod sursa(job #1604154)

Utilizator aimrdlAndrei mrdl aimrdl Data 17 februarie 2016 23:57:15
Problema Secventa Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <iostream>
#include <deque>
 
using namespace std;
 
struct item {
    int val;
    int pos;
};
 
int main (void) {
    ios_base::sync_with_stdio(false);
     
    freopen("secventa.in", "r", stdin);
    freopen("secventa.out", "w", stdout);
         
    int n, k;
     
    cin >> n >> k;
    int v[n];
     
    deque<item> l;
     
    item aux;
    int max = 0, endSol = 0;
     
    for (int i = 0; i < k; ++i) {
        cin >> v[i];
        while (!l.empty() && v[i] <= l.back().val) {
            l.pop_back();
        }
         
        aux.val = v[i];
        aux.pos = i;
        l.push_back(aux);
    }
         
         
    max = l.front().val;
    endSol = l.front().pos;
     
    for (int i = k; i < n; ++i) {    
        cin >> v[i];
        while (!l.empty() && v[i] <= l.back().val) {
            l.pop_back();
        }
         
        aux.val = v[i];
        aux.pos = i;
        l.push_back(aux);
         
        if (l.front().pos <= i - k) l.pop_front();
         
        if (l.front().val > max) {
            max = l.front().val;
            endSol = i;
        }
    }
     
    cout << endSol - k + 2 << " " << endSol + 1 << " " << max;
     
    return 0;
}