Cod sursa(job #1711004)

Utilizator iulian_f2kGuraliuc Iulian iulian_f2k Data 30 mai 2016 10:10:20
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.17 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
/*
struct node {
    int val;
    node *prev, *next;
};
class deq {
    node *first, *last;
public:
    deq();
    ~deq();
    bool empty() {  return (first == NULL && last == NULL);     }
    int Back();
    int Front();
    void pushBack(int &x);
    void pop_Back();
    void pushFront(int &x);
    void pop_Front();
};
deq :: deq() {
    first = NULL;
    last = NULL;
}
deq :: ~deq() {
    node *q, *p = first;
    while(p->next != NULL) {
        q = p;
        p = p->next;
        delete q;
    }
    first = last = NULL;
}
int deq :: Back() {
    return last->val;
}
int deq :: Front() {
    return first->val;
}
void deq :: pushBack(int &x) {
    node *newNode = new node;
    newNode->val = x;
    newNode->next = NULL;
    newNode->prev = last;
    last = newNode;
    if(first == NULL)
        first = last;
    else
        last->prev->next = last;
}
void deq :: pushFront(int &x) {
    node *newNode = new node;
    newNode->val = x;
    newNode->next = first;
    newNode->prev = NULL;
    first = newNode;
    if(last == NULL)
        last = first;
    else
        first->next->prev = first;
}
void deq :: pop_Back() {
    if(last == NULL)
        return;
    else if(first->val == last->val) {
        node *p = last;
        first = last = NULL;
        delete p;
    }
    else if(first->next == last) {
        node *p = last;
        first->next = NULL;
        last = first;
        delete p;
    }
    else {
        node *p = last;
        last = last->prev;
        last->next = NULL;
        delete p;
    }
}
void deq :: pop_Front() {
    if(first == NULL)
        return;
    else if(first->val == last->val) {
        node *p = first;
        first = last = NULL;
        delete p;
    }
    else if(first->next == last) {
        node *p = first;
        first = first->next;
        first->prev = NULL;
        delete p;
    }
    else {
        node *p = first;
        first = first->next;
        first->prev = NULL;
        delete p;
    }
}

int V[5000005];
deq D;
int N, K;
long long sol;
int main()
{
    freopen("deque.in", "rt", stdin);
    freopen("deque.out", "wt", stdout);

    scanf("%d%d", &N, &K);
    for(int i = 0; i < N; ++i) {
        scanf("%d", &V[i]);
        while(!D.empty() && V[i] <= V[ D.Back() ])
            D.pop_Back();
        D.pushBack(i);
        if(!D.empty() && i - K == D.Front())
            D.pop_Front();
        if(i >= K - 1)
            sol += (long long)V[ D.Front() ];
    }
    cout << sol << '\n';
    return 0;
}
*/
#include <deque>
vector<int> V;
deque<int> D;
int N, K;
long long sol;
int main()
{
    freopen("deque.in", "rt", stdin);
    freopen("deque.out", "wt", stdout);
    scanf("%d%d", &N, &K);
    V.assign(N+2, 0);
    for(int i = 1; i <= N; ++i) {
        scanf("%d", &V[i]);
        while(!D.empty() && V[i] <= V[ D.back() ])
            D.pop_back();
        D.push_back(i);
        if(i - K == D.front())
            D.pop_front();
        if(i >= K)
            sol += V[ D.front() ];
    }
    cout << sol << '\n';
}