Cod sursa(job #2884784)

Utilizator EduardSanduSandu Eduard Alexandru EduardSandu Data 4 aprilie 2022 21:16:06
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.94 kb
#include<bits/stdc++.h>

using namespace std;

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

struct nod
{
    pair<int, int> value;
    nod * prev;
    nod * next;
};

class Eduard_Deque
{
private:
    nod * front;
    nod * back;
    long long size;

public:
    Eduard_Deque()
    {
        front = back = 0;
        size = 0;
    }

    bool isEmpty()
    {
        if(size == 0)
            return 1;
        return 0;
    }

    void push_front(int number, int index)
    {
        if(this->size == 0)
        {
            nod * x = new nod;
            x->value = {number, index};
            x->prev = 0;
            x->next = 0;
            this->front = x;
            this->back = x;
            this->size++;
        }
        else
        {
            nod * x = new nod;
            x->value = {number, index};
            x->next = front;
            front->prev = x;
            front = x;
            this->size++;
        }
    }

    void pop_front()
    {
        if(this->size == 1)
        {
            this->size--;
            delete front;
            front = 0;
            back = 0;
        }
        else if(this->size > 1)
        {
            front = front->next;
            delete front->prev;
            front->prev = 0;
            this->size--;
        }
    }

    void push_back(int number, int index)
    {
        if(this->size == 0)
        {
            nod * x = new nod;
            x->value = {number, index};
            x->prev = 0;
            x->next = 0;
            this->front = x;
            this->back = x;
            this->size++;
        }
        else
        {
            nod * x = new nod;
            x->value = {number, index};
            x->prev = back;
            back->next = x;
            back = x;
            this->size++;
        }
    }

    void pop_back()
    {
        if(this->size == 1)
        {
            this->size--;
            delete back;
            front = 0;
            back = 0;
        }
        else if(this->size > 1)
        {
            back = back->prev;
            delete back->next;
            back->next = 0;
            this->size--;
        }
    }

    pair<int, int> front_element()
    {
        if(this->size > 0)
            return front->value;
    }

    pair<int, int> back_element()
    {
        if(this->size > 0)
            return back->value;
    }
};

int main()
{
    Eduard_Deque dq;
    int n,i,j,k,nr;
    long long suma = 0;
    fin>>n>>k;
    for(i=1;i<=n;i++)
    {
        fin>>nr;
        while(!dq.isEmpty() && dq.back_element().first >= nr)
        {
            dq.pop_back();
        }
        dq.push_back(nr, i);
        if(i >= k)
            suma += dq.front_element().first;
        if(i >= dq.front_element().second + k - 1){
            dq.pop_front();
        }
    }
    fout<<suma;
    return 0;
}