Cod sursa(job #2544960)

Utilizator marius004scarlat marius marius004 Data 12 februarie 2020 18:45:46
Problema Secventa Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <deque>

std::ifstream f("secventa.in");
std::ofstream g("secventa.out");

const int NMAX = 500'005;
int n,k,v[NMAX],sol,I,J,maxx,maxxIndex;
std::deque<int>d;

int main(){
    
    f >> n >> k;
    
    for(int i = 1;i <= n;++i){
        
        f >> v[i];
        
        while(!d.empty() && v[i] <= v[d.back()])
            d.pop_back();
        
        d.push_back(i);
        
        if(i - d.front() + 1 > k)
            d.pop_front();
        
        if(i >= k && v[d.front()] > maxx){
            maxx = v[d.front()];
            maxxIndex = d.front();
        }
    }
    
    // o solutie doar ca nu e optima
    I = maxxIndex;
    J = maxxIndex + k - 1;
    
    //incerc sa iau solutia optima
    
    int Ioptim = I;
    int Joptim = J;
    
    while(v[Ioptim - 1] >= maxx)
        Ioptim--;
    
    g << Ioptim << ' ' << Joptim << ' ' << maxx;
    
    return 0;
}