Mai intai trebuie sa te autentifici.

Cod sursa(job #2938139)

Utilizator vlad414141414141Vlad Ionescu vlad414141414141 Data 11 noiembrie 2022 18:15:54
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
#include <deque>

using namespace std;

ifstream fin ("deque.in");
ofstream fout ("deque.out");

deque <int> v;
int n, k, x[5000041], s;

void read()
{
    fin >> n >> k;
    for (int i=0;i<n;++i)
        fin >> x[i];
    v.push_front(0);
    for (int i=1;i<k;++i)
    {
        if(x[i]<x[v.front()])
        {
            v.clear();
            v.push_front(i);
        }
        else
        {
           while(x[i]<x[v.back()])
            {
                v.pop_back();
            }
            v.push_back(i);
        }
    }
   // fout << x[v.front()] << " ";
           s+=x[v.front()];

    for (int i=k;i<n;++i)
    {
        if (v.front()<i-k+1)
            v.pop_front();
        if (x[i]<x[v.front()])
        {
            v.clear();
            v.push_front(i);
        }
        else{
            while(x[i]<x[v.back()])
            {
                v.pop_back();
            }
            v.push_back(i);
        }
        //fout << x[v.front()] << " ";
        s+=x[v.front()];
    }
    fout << s;
}

int main()
{
    read();
    return 0;
}