Cod sursa(job #2547268)

Utilizator marius004scarlat marius marius004 Data 15 februarie 2020 10:46:44
Problema Secventa Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <deque>

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

const int NMAX = 500005;
int n,k,v[NMAX],sol,I,J,maxx = -(1 << 30);
std::deque<int>d;

int getDistance(int x,int y){
	return y - x + 1;
}

int main(){

    std::ios_base::sync_with_stdio(false);

    f >> n >> k;

    // din fiecare secventa de lungime k iau minimul si il compar cu maximul curent
    //numarul obtinut este valoarea maxima a bazei.mai trebuie sa iau indexul minim
    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()];
            J = i;
            I =  i - k + 1;
        }
    }

    g << I << ' ' << J << ' ' << maxx;

    return 0;
}