Cod sursa(job #2204771)

Utilizator DordeDorde Matei Dorde Data 16 mai 2018 23:29:56
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <fstream>
#include <cstdio>
#include <deque>
#include <ctype.h>
using namespace std;
deque <int> q;
typedef long long ll;
char const in [] = "deque.in";
char const out [] = "deque.out";
int const NM = 5e6 + 7 , BUFF = 10;
char buff [BUFF];
int point;
ll getbuff ()
{
    ll nr = 0;
    bool semn = 0;
    while(! isdigit (buff [point]))
    {
        if(buff [point] == '-')
            semn = 1;
        ++ point;
        if(point == BUFF)
        {
            point = 0;
            fread (buff , 1 , BUFF , stdin);
        }
    }
    while(isdigit (buff [point]))
    {
        nr = nr * 10 + (buff [point] - '0');
        ++ point;
        if(point == BUFF)
        {
            point = 0;
            fread (buff , 1 , BUFF , stdin);
        }
    }
    if(semn)
        nr = 0 - nr;
    return nr * 1LL;
}
ll v [NM] , val;
int main()
{
    freopen (in , "r" , stdin);
    freopen (out , "w" , stdout);
    fread (buff , 1 , BUFF , stdin);
    ll n ;
    n = getbuff ();
    ll k = getbuff ();
    for(int i = 1 ; i <= n ; ++ i)
        v [i] = getbuff ();
    //q . push_front (1);
    for(int i = 1 ; i <= n ; ++ i)
    {
        while(! q . empty () && v [q . back ()] >= v [i])
            q . pop_back ();
        if (! q . empty () && q . front () <= i - k)
            q . pop_front ();
        q . push_back (i);
        if(i >= k)
            val = 1LL * (val + v [q . front ()]);
    }
    printf ("%lld" , val);
    return 0;
}