Cod sursa(job #3127104)

Utilizator DariusGhercaDarius Gherca DariusGherca Data 7 mai 2023 12:17:51
Problema Deque Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.15 kb
#include <iostream>
#include <fstream>
#include <climits>
#define cout df
using namespace std;
ofstream df("dbg.out");
struct nod{
    int val;
    bool ok = true;
    nod *left, *right;
};

class adeque{
    nod *top, *bot;
    int mini = INT_MAX;
public:
    adeque(int x){
        top = new nod;
        top -> val = x;
        top -> right = nullptr;
        top -> left = nullptr;
        bot = top;
        mini = x;
    }
    void push_top(int x){
        nod* aux = new nod;
        aux -> val = x;
        aux -> left = nullptr;
        aux -> right = top;
        top -> left = aux;
        top = aux;
        mini = min(mini, x);
    }
    int pop_bot(){
        nod *aux = bot -> left;
        int rez = bot -> val;
        aux -> right = nullptr;
        delete bot;
        bot = aux;
        if(rez == mini)
            act_min();
        return rez;
    }
    void act_min(){
        nod *aux = top;
        mini = INT_MAX;
        while(aux){
            mini = min(mini, aux -> val);
            aux = aux -> right;
        }
    }
    int get_min(){
        return mini;
    }
    void afis(){
        nod *aux = top;
        cout << "\n";
        cout << "T " << top -> val << " B " << bot -> val << "\n";
        int cnt = 0;
        while(aux && cnt <= 100){
            cout << aux -> val << " ";
            aux = aux -> right;
            cnt++;
        }
        cout << "\n";
        aux = bot;
        cnt = 0;
        while(aux && cnt <= 100){
            cout << aux -> val << " ";
            aux = aux -> left;
            cnt++;
        }
        cout << "\n";
    }
};

int main()
{
    int n, k;
    ifstream f("deque.in");
    f >> n >> k;
    int x;
    f >> x;
    adeque m(x);
    for(int i = 2; i <= k; i++){
        f >> x;
        m.push_top(x);
    }
    long long sum = 0;
    for(int i = k+1; i <= n; i++){
        //cout << m.get_min() << "\n";
        sum += m.get_min();
        m.pop_bot();
        f >> x;
        m.push_top(x);
    }
    sum += m.get_min();
    //cout << m.get_min();
    ofstream g("deque.out");
    g << sum;
    return 0;
}