Cod sursa(job #1058454)

Utilizator romykPrehari Romica romyk Data 15 decembrie 2013 16:03:56
Problema Deque Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <stdio.h>
#include <malloc.h>
using namespace std;
struct node{
    int info;
    struct node *next;
}*first,*p;

long a[5000000];

void add(int inf)
{
   node *n=(node *)malloc(sizeof(node));
   n->info=inf;
   if(first!=NULL){
        p=first;
        if(first->info>inf){
            n->next=first;
            first=n;
        }
        else{
            while(p->next!=NULL&&p->next->info<inf)p=p->next;
            n->next=p->next;
            p->next=n;
       }
   }
   else{
       first=n;
       first->next=NULL;
   }
}
int search(int info)
{
    p=first;
    while(p!=NULL&&p->info!=info)p=p->next;
    if(p!=NULL)
        return 1;    else
        return 0;
}
void del(int info)
{

   node *n;
   if(first!=NULL){
       p=first;
       if(first->info==info){
             p=first;
             first=first->next;
             delete p;
       }
       else{
            while(p->next!=NULL&&p->next->info!=info)p=p->next;
            if(p->next!=NULL)
            {n=p->next;
            p->next=p->next->next;}
            delete n;
       }
   }
}

int main()
{
    int i,n,k,j,ll;

    long long s=0;
   freopen("deque.in","r",stdin);
   freopen("deque.out","w",stdout);
   scanf("%i %i",&n,&k);
    for(i=0;i<n;i++)
    scanf("%li",&a[i]);
for(i=0;i<k;i++)
    add(a[i]);
s+=first->info;
for(i=k;i<n;i++)
{
    del(a[i-k]);
    add(a[i]);
    s+=first->info;
}


printf("%lli",s);

    return 0;
}