Cod sursa(job #2724198)

Utilizator realmeabefirhuja petru realmeabefir Data 16 martie 2021 18:23:56
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.2 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f ("dequeue.in");
ofstream g ("dequeue.out");

#define MAX_SIZE 1

class Queue{
    int *data;
    int first,last,alocate,_size;
public:
    Queue();
    ~Queue();
    int get_first();
    int get_first_index();
    int get_last();
    int get_last_index();
    int get_size();
    void push(int);
    void pop();
    void pop_back();
    friend ostream& operator << (ostream &, Queue &);
};

Queue::Queue(){
    alocate = MAX_SIZE;
    data = new int[MAX_SIZE];
    first = 0;
    last = -1;
    _size = 0;
}

Queue::~Queue(){
    delete []data;
}

int Queue::get_first(){
    if (_size)
        return data[first];
}

int Queue::get_first_index(){
    if (_size)
        return first;
}

int Queue::get_last(){
    if (_size)
        return data[last];
}

int Queue::get_last_index(){
    if (_size)
        return last;
}

int Queue::get_size(){
    return _size;
}

void Queue::push (int x){
    if (_size == alocate){
        //cout << " aloca extra \n";
        int *new_data = new int[alocate*2];
        int k = 0;
        if (last < first){
            for (int i = first; i < alocate; i++)
                new_data[k++] = data[i];
            for (int i = 0; i <= last; i++)
                new_data[k++] = data[i];
        } else {
            for (int i = first; i <= last; i++){
                new_data[k++] = data[i];
            }
        }
        alocate *= 2;
        _size = k;
        first = 0;
        last = _size-1;
        delete []data;
        data = new_data;

        data[++last] = x;
        _size ++;
    } else {
        if (last == alocate-1){
            last = 0;
            data[last] = x;
            _size ++;
        } else {
            data[++last] = x;
            _size ++;
        }
    }
}

void Queue::pop(){
    if (!_size){
        return;
    }

    if (first == alocate-1){
        first = 0;
    } else {
        first ++;
    }
    _size --;

    if (_size < alocate/4){
        //cout << " dezaloca \n";
        int *new_data = new int[alocate/2];
        int k = 0;
        if (last < first){
            for (int i = first; i < alocate; i++)
                new_data[k++] = data[i];
            for (int i = 0; i <= last; i++)
                new_data[k++] = data[i];
        } else {
            for (int i = first; i <= last; i++){
                new_data[k++] = data[i];
            }
        }
        alocate /= 2;
        _size = k;
        first = 0;
        last = _size-1;
        delete []data;
        data = new_data;
    }

    if (_size == 0){
        first = 0;
        last = -1;
    }
}

void Queue::pop_back(){
    if (!_size){
        return;
    }

    if (last == 0 && first > last){
        last = alocate-1;
    } else {
        last --;
    }
    _size --;

    if (_size == 0){
        first = 0;
        last = -1;
    }
}

ostream& operator << (ostream &os, Queue &q){
    if (q._size == 0){
        os << " coada este goala!\n";
        return os;
    }

    if (q.first <= q.last){
        for (int i = 0; i < q.first; i++)
            cout << '#' << ' ';
        for (int i = q.first; i <= q.last; i++)
            cout << q.data[i] << ' ';
        for (int i = q.last+1; i < q.alocate; i++)
            cout << '#' << ' ';
    } else {
        for (int i = 0; i <= q.last; i++)
            cout << q.data[i] << ' ';
        for (int i = q.last+1; i < q.first; i++)
            cout << '#' << ' ';
        for (int i = q.first; i < q.alocate; i++)
            cout << q.data[i] << ' ';
    }
    os << '\n';
    return os;
}

int main()
{
    Queue q;
    Queue poz;

    int n,s;
    f >> n >> s;
    int x;
    int suma = 0;

    for (int i = 1; i <= n; i++){
        f >> x;
        while (q.get_size() && x <= q.get_last()){
            q.pop_back();
            poz.pop_back();
        }

        q.push(x);
        poz.push(i);

        if (poz.get_first() == i-s){
            q.pop();
            poz.pop();
        }

        if (i >= s)
            suma += q.get_first();
    }

    cout << suma;

    return 0;
}