Cod sursa(job #1616846)

Utilizator PraetorGrigorosoaia Florin Praetor Data 27 februarie 2016 12:05:31
Problema Divk Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 kb
#include<fstream>
#define NRMR 100000 // numarul maxim de resturi

using namespace std;

FILE*in;
ofstream out("divk.out");

long int nr_elemente;
long int A, B; // capetele intervalului
long int K;
long int X; // un element din vector
long long S;
long long nr_subsecvente;

struct LSI
{
    long int info;
    LSI *next;
}*head[NRMR], *tail[NRMR];

void add_elem(long int rest, long int elem)
{
    LSI *now[NRMR];

    if (!head[rest])
    {
        head[rest]=new LSI;

        head[rest]->info=elem;

        tail[rest]=head[rest];
    }
    else
    {
        now[rest]=new LSI;

        now[rest]->info=elem;

        tail[rest]->next=now[rest];
        tail[rest]=now[rest];
    }

    tail[rest]->next=0;
}

/*void show(long int rest)
{
    LSI *now[NRMR];
    now[rest]=head[rest];

    while (now[rest])
    {
        out<<now[rest]->info<<" ";

        now[rest]=now[rest]->next;
    }
}*/

void read()
{
    in=fopen("divk.in", "r");

    fscanf(in, "%ld%ld%ld%ld", &nr_elemente, &K, &A, &B);
    for (long int i=1; i<=nr_elemente; i++)
    {
        fscanf(in, "%ld", &X);

        S+=X;

        add_elem(S%K, i);
    }
    A--;
}

void count_subsecvente(long int rest)
{
    if ((head[rest]) && (head[rest] != tail[rest]))
    {
        LSI *now[NRMR];
        now[rest]=head[rest]->next;

        while ((now[rest] != tail[rest]) || (head[rest] != tail[rest]))
        {
            long int dif=now[rest]->info-head[rest]->info;

            if ((A <= dif) && (dif <= B))
            {
                nr_subsecvente++;

                if ((head[rest]->next != now[rest]) || (now[rest] == tail[rest]))
                    head[rest]=head[rest]->next;
                else
                    now[rest]=now[rest]->next;
            }
            else if (dif < A)
            {
                if ((now[rest] != tail[rest]) || (head[rest]->next == now[rest]))
                    now[rest]=now[rest]->next;
                else
                    head[rest]=head[rest]->next;

            }
        }
    }
}

void solve()
{
    for (long int i=0; i<K; i++)
        count_subsecvente(i);
}

void show()
{
    out<<nr_subsecvente;
}

int main()
{
    read();
    solve();
    show();

    return 0;
}