Cod sursa(job #3125547)

Utilizator DevCrutCorolevschi Mihai DevCrut Data 3 mai 2023 18:05:25
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.17 kb
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") 

#include <iostream>
#include <fstream>

#define MAX_COUNT 5000000

using namespace std;

class Deque {
private:
    int* elems;
    int start, end, size, capacity;
public:
    Deque(int elemCount) {
        this->elems = new int[elemCount];
        this->size = elemCount;
        this->start = 0;
        this->end = -1;
        this->capacity = 0;
    }
    ~Deque() {
        delete this->elems;
    }

    int getElemsCount() {
        return this->end - this->start;
    }
    bool isFull() {
        return this->capacity == this->size;
    }
    bool isEmpty() {
        return this->capacity == 0;
    }

    void pushFront(int elem) {
        if (this->isFull() == true)
            return;

        if (this->isEmpty()) {
            this->start = this->end = 0;
            this->elems[this->end] = elem;
        }
        else {
            this->end++;
            if (this->end >= this->size) {
                this->end = 0;
            }
            this->elems[this->end] = elem;
        }
        this->capacity++;
    }
    void pushBack(int elem) {
        if (this->isFull() == true)
            return;

        if (this->isEmpty()) {
            this->start = this->end = 0;
            this->elems[this->start] = elem;
        }
        else {
            this->start--;
            if (this->start < 0) {
                this->start = this->size - 1;
            }
            this->elems[this->start] = elem;
        }
        this->capacity++;
    }

    void popFront() {
        if (this->isEmpty() == true)
            return;

        this->end--;
        this->capacity--;
        if (this->end < 0)
            this->end = this->size - 1;
    }
    void popBack() {
        if (this->isEmpty() == true)
            return;

        this->start++;
        this->capacity--;
        if (this->start >= this->size)
            this->start = 0;
    }

    int front() {
        if (this->isEmpty() == true)
            return -1;
        return this->elems[end];
    }
    int back() {
        if (this->isEmpty() == true)
            return -1;
        return this->elems[start];
    }

    void view() {
        for (int i = 0; i < this->capacity; ++i) {
            cout << (this->elems[(this->start + i) % this->capacity]) << ' ';
        }
        cout << '\n';
    }
};

ifstream myIn("deque.in");
ofstream myOut("deque.out");

int vec[MAX_COUNT];

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    long long n, k, sum = 0;
    myIn >> n >> k;
    Deque deque(n);
    for (int i = 0; i < n; ++i)
        myIn >> vec[i];
    for (int i = 0; i < n; ++i) {
        while (!deque.isEmpty() && vec[deque.front()] > vec[i])
            deque.popFront();
        while (!deque.isEmpty() && i - deque.back() >= k)
            deque.popBack();
        deque.pushFront(i);
        //deque.view();
        if (i >= k - 1) {
            //cout << vec[deque.back()] << endl;
            sum += vec[deque.back()] * 1LL;
        }
    }
    myOut << sum;
    return 0;
}