Cod sursa(job #1072850)

Utilizator IeewIordache Bogdan Ieew Data 4 ianuarie 2014 23:57:08
Problema Deque Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.81 kb
#include <fstream>

using namespace std;

#define DEBUG true

#if DEBUG
#include <iostream>
#define INFILE "/Users/biordach/Documents/Xcode Projects/training/training/deque.in"
#define __OUT cout
#else
#define INFILE "deque.in"
#define OUTFILE "deque.out"
ofstream __OUT(OUTFILE);
#endif

void readInput();
void solve();
void printOutput();

struct node{
    int value, position;
    node *prev, *next;
}*first, *last;

int main(int argc, const char * argv[])
{
    readInput();
//  solve();
//  printOutput();

#if DEBUG == false
    __OUT.close();
#endif
    
    return 0;
}

node* createNode(int value, int pos){
    node* q = new node;
    q->prev = q->next = NULL;
    q->value = value;
    q->position = pos;
    return q;
}

void deleteList(){
    node* q = first;
    while(q){
        node* next = q->next;
        delete q;
        q = next;
    }
    first = last = NULL;
}

void addToList(int x, int pos){
    node* newNode = createNode(x, pos);
    
    if(first != NULL && first -> value >= x){
        deleteList();
    }
    
    if(last == NULL){
        first = last = newNode;
        return;
    }
    
    while(last -> value >= x){
        node* prev = last->prev;
        delete last;
        last = prev;
    }
    last->next = newNode;
    newNode->prev = last;
    last = newNode;
}

void readInput(){
    int n, k;
    long long sum = 0;
    ifstream in(INFILE);
    in>>n>>k;
    int x;
    for(int i=0;i<k;i++){
        in>>x;
        addToList(x, i);
    }
    
    sum += first->value;
    for(int i=k; i < n; i++){
        in>>x;
        addToList(x, i);
        if(i - first->position == k){
            node* next = first->next;
            delete first;
            first = next;
        }
        sum += first->value;
    }
    
    
    in.close();
    __OUT<<sum<<'\n';
}

void solve(){
}

void printOutput(){
}