Cod sursa(job #2017205)

Utilizator LucaSeriSeritan Luca LucaSeri Data 31 august 2017 15:30:51
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.97 kb
#include <fstream>
#include <cassert>
#include <vector>
using namespace std;

ifstream cin("deque.in");
ofstream cout("deque.out");

class Deque{
public:
    long long Solve(int n, int k){
        long long ans = 0;
        for(int i = 0, a; i < k; ++i){
            cin >> a;
            if(this -> size() == 0){
                this -> push_front(a, i);
            }
            else{
                if(a > this -> back -> val && i > n-k+1) continue;
                while(this -> empty() == false && a <= this -> back -> val) this -> pop_back();
                this -> push_back(a, i);
            }
        }
        ans += this -> front -> val;

        for(int i = k, a; i < n; ++i){
            cin >> a;
            while(this -> empty() == false && a <= this -> back -> val) this -> pop_back();
            this -> push_back(a, i);
            while(this -> empty() == false && this -> front -> ind <= i - k) this -> pop_front();
            ans += this -> front -> val;
        }

        return ans;
    }

private:
    int  sz = 0;
    struct Node{
        Node(int x, int y){
            this -> val = x;
            this -> ind = y;
            this -> next = NULL;
            this -> prev = NULL;
        }

        int val;
        int ind;
        Node* next;
        Node* prev;
    };

    Node *back = NULL;
    Node *front = NULL;

    void push_front(int val, int ind){
        if(sz == 0){
            Node* n = new Node(val, ind);
            back = n;
            front = n;
            ++sz;
        }
        else{
            Node* n = new Node(val, ind);
            front -> next = n;
            n -> prev = front;
            front = n;
            ++sz;
        }
    }

    void push_back(int val, int ind){
        if(sz == 0){
            Node* n = new Node(val, ind);
            back = n;
            front = n;
            ++sz;
        }
        else{
            Node* n = new Node(val, ind);
            back -> prev = n;
            n -> next = back;
            back = n;
            ++sz;
        }
    }


    void pop_front(){
        assert(sz > 0);
        if(sz == 1){
            sz = 0;
            delete front;
            back = NULL;
            front = NULL;
        }
        else {
            --sz;
            front = front -> prev;
            delete front -> next;
            front -> next = NULL;
        }

    }

    void pop_back(){
        assert(sz > 0);
        if(sz == 1){
            sz = 0;
            delete back;
            back = NULL;
            front = NULL;
        }
        else{
            -- sz;
            back = back -> next;
            delete back -> prev;
            back -> prev = NULL;
        }
    }

    int size(){
        return sz;
    }

    bool empty(){
        return sz == 0;
    }
}deq;

int main(){
    int n;
    int k;
    cin >> n >> k;
    cout << deq.Solve(n, k);
    return 0;
}