Cod sursa(job #2472582)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 12 octombrie 2019 16:45:44
Problema Reguli Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

long long a[500000];
size_t aux[500000];

int main()
{
    ifstream fin("reguli.in");
    ofstream fout("reguli.out");

    size_t n;
    fin >> n;

    long long x_curent, x_anterior;

    fin >> x_anterior >> x_curent;
    a[0] = x_curent - x_anterior;
    x_anterior = x_curent;

    for(size_t k = 0, i = 1; i <= n - 2; i++)
    {
        fin >> x_curent;
        a[i] = x_curent - x_anterior;
        x_anterior = x_curent;

        while(k > 0 && a[i] != a[k]) k = aux[k - 1];

        if(a[i] == a[k]) ++k;

        aux[i] = k;
    }

    size_t pos;
    for(pos = n - 2; pos > 0 && aux[pos] > aux[pos - 1]; pos-- );

    ++pos;

    fout << pos << '\n';

    for(size_t i = 0; i < pos; ++i) fout << a[i] << '\n';
}