Cod sursa(job #2724326)

Utilizator PopescuMihneaPopescu Mihnea-Valentin PopescuMihnea Data 16 martie 2021 22:27:47
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.01 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("deque.in");
ofstream g ("deque.out");
class Node
{
    int inf,poz;
    Node *next,*prev;
public:
    Node();
    Node (int,int);
    ~Node();
    friend class Dequeue;
};

Node::Node(): inf(0),poz(-1),next(NULL),prev(NULL)
{

}

Node::Node(int inf,int poz): inf(inf),poz(poz),next(NULL),prev(NULL)
{

}

Node::~Node()
{
    delete next;
    delete prev;
}

class Dequeue
{
    Node *first,*last;
    int qsize;
public:
    Dequeue();
    int get_first() const;
    int get_poz_first() const;
    int get_last() const;
    int get_poz_last() const;
    void push_first(int,int);
    void push_last(int,int);
    void pop_first();
    void pop_last();
    int get_size();
    ~Dequeue();

};

Dequeue::Dequeue(): first(NULL),last(NULL),qsize(0)
{

}


Dequeue::~Dequeue()
{

}

int Dequeue::get_first() const
{
    return this->first->inf;
}

int Dequeue::get_poz_first() const
{
    return this->first->poz;
}

int Dequeue::get_last() const
{
    return this->last->inf;
}

int Dequeue::get_poz_last() const
{
    return this->last->poz;
}

void Dequeue::push_first(int inf,int poz)
{
    Node *q;
    q=new Node(inf,poz);
    q->next=first;
    if (first!=NULL)
        this->first->prev=q;
    this->first=q;
    if (last==NULL)
        this->last=first;
    this->qsize++;
    q=new Node;
    delete q;
}

void Dequeue::push_last(int inf,int poz)
{
    Node *q;
    q=new Node;
    q->inf=inf;
    q->poz=poz;
    q->prev=last;
    if (this->last!=NULL)
        this->last->next=q;
    this->last=q;
    if (first==NULL)
        this->first=last;
    this->qsize++;
    q=new Node;
    delete q;
}

void Dequeue::pop_first()
{
    Node *aux;
    if(first!=NULL)
    {
        aux=this->first;
        if (this->first->next!=NULL)
        {
            this->first=this->first->next;
            this->first->prev=NULL;
        }
        else
            this->first=this->last=NULL;
        aux->next=NULL;
        aux->prev=NULL;
        this->qsize--;
    }
    delete aux;
}

void Dequeue::pop_last()
{
    Node *aux;
    if (last!=NULL)
    {
        aux=this->last;
        if (this->last->prev!=NULL)
        {
            this->last=this->last->prev;
            this->last->next=NULL;
        }
        else
            this->last=this->first=NULL;
        aux->next=NULL;
        aux->prev=NULL;
        this->qsize--;
    }
    delete aux;
}

int Dequeue::get_size()
{
    return this->qsize;
}

int main()
{
    int N,K,x;
    long long sol=0;
    Dequeue coada;
    f>>N>>K;
    for (int i=1; i<=N; i++)
    {
        f>>x;
        while (coada.get_size()!=0 && coada.get_poz_first()<=i-K)
            coada.pop_first();
        while (coada.get_size()!=0 && coada.get_last()>=x)
            coada.pop_last();
        coada.push_last(x,i);
        if (i-K>=0)
            sol+=coada.get_first();
    }
    g<<sol;
}