Cod sursa(job #1450884)

Utilizator azkabancont-vechi azkaban Data 15 iunie 2015 00:19:29
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 2.62 kb
#include <bits/stdc++.h>
using namespace std;

class double_ended_queue
        {
    private :
               struct celula
                   {
                   int val;
                   int ind;
                   celula *next;
                   celula *prev;
                   };

               typedef celula *adresa;
               adresa top=NULL, bottom=NULL;

       public :
                void pushback(int value, int indice)
                   {
                    adresa cell;
                    cell=new celula;
                    cell->val=value;
                    cell->ind=indice;
                    cell->next=NULL;

                    if (bottom==NULL)
                         {
                         cell->prev=NULL;
                         top=cell;
                         bottom=cell;
                         }
                       else
                        {
                        cell->prev=bottom;
                        bottom->next=cell;
                        bottom=cell;
                       }
                   }

                 bool empty()
                      {
                       if (top==NULL || bottom==NULL) return 1;
                       return 0;
                      }

                void popfront()
                   {
                    adresa x;
                    if (top!=NULL)
                      {
                      x=top;
                      (top->next)->prev=NULL;
                      top=top->next;
                      delete(x);
                      }
                   }

                void popback()
                   {
                    adresa x;
                    if (bottom && top)
                      {
                      x=bottom;
                      if (bottom->prev!=NULL) bottom->prev->next=NULL;
                      bottom=bottom->prev;
                      delete(x);
                      }
                   }
                adresa front(){ if (top!=NULL) return top; return NULL; }
                adresa back (){ if (bottom!=NULL) return bottom; return NULL; }
        };

double_ended_queue Deq;
long long sum(0),n,k,aux,pivot(0),i;

int main(void)
{
 ifstream cin("deque.in");
 ofstream cout("deque.out");
 cin>>n>>k;
 for (i=1;i<=n;++i)
   {
    cin>>aux;
    while(!Deq.empty() && Deq.back()->val >= aux) Deq.popback();
    Deq.pushback(aux,i);
   if (i>=k)
         {
          ++pivot;
          sum+=Deq.front()->val;
          while(Deq.front()->ind <= pivot) Deq.popfront();
         }
   }
 cout<<1LL*sum<<"\n";
 return 0;
}