Cod sursa(job #1784521)

Utilizator teo.cons98Constantin Teodor-Claudiu teo.cons98 Data 20 octombrie 2016 10:03:42
Problema Deque Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
struct nod
{
    int val;
    nod* nxt;
} *p, *u;

void adaugare(int x)//BUNA
{
    nod *k;
    k = new(nod);
    k->val = 0;
    u->val = x;
    u->nxt = k;
    u = k;
}

int min1()//BUNA
{
    int mink;
    nod *k;
    k = new(nod);
    k = p;
    mink = k->val;
    k = k->nxt;
    while(k != u)
    {
        if(k->val < mink)
        {
            mink = k->val;
        }
        k = k->nxt;
    }
    return mink;
}

void stergere()// BUNA
{
    nod *k;
    k = p;
    p = p->nxt;
    delete(k);
}
int main()
{
    int n,k,x,contor = 0;
    long long minim1 = 0, minimk = 10000005, e = 0;

    fin>>n>>k;

    p = new(nod);
    u = new(nod);

    p = u;

    for(int i = 1;i <=n ;i++)
    {
         e = 0;
        fin>>x;
        if(contor == k)
        {
            if(p->val == minimk) e = 1;
            stergere();
            contor--;
        }
        while(x <= p->val and p != u)
        {
            if(p->val == minimk)
            {
                e = 0;
                minimk = x;
            }
            stergere();
            contor--;
        }
        adaugare(x);
        contor++;
        /*if(i == k)
        {
            minimk = min1();
        }*/
            minimk = min1();
        if(i >= k)
        {
            minim1 += minimk;
        }
    }
    fout<<minim1;
}