Cod sursa(job #2349331)

Utilizator cristina_borzaCristina Borza cristina_borza Data 20 februarie 2019 13:21:11
Problema Numarare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f ("numarare.in");
ofstream g ("numarare.out");

const int Dim = 1e5 + 5;

int a[Dim], v[Dim];
int n;

long long ans;

int main() {
    f >> n;
    for (int i = 1; i <= n; ++ i) {
        f >> a[i];
    }

    for (int i = 1; i <= n; ++ i) {
        v[i] = a[i + 1] - a[i];
    }

    memset (a, 0, sizeof (a));

    -- n;
    int pos = -1, lg = 0;
    for (int i = 1; i <= n; ++ i) {
        if (pos + lg < i) {
            lg = 0;
            while (i + lg <= n && i - lg > 0 && v[i + lg] == v[i - lg]) {
                ++ lg;
            }

            -- lg;

            a[i] = lg;
            pos = i;
        }

        else {
            int p = 2 * pos - i;
            if (a[p] < pos + lg - i) {
                a[i] = a[p];
            }

            else {
                if (a[p] > pos + lg - i) {
                    a[i] = pos + lg - i;
                }

                else {
                    lg = a[p];
                    while (i + lg <= n && i - lg > 0 && v[i - lg] == v[i + lg]) {
                        ++ lg;
                    }

                    -- lg;

                    a[i] = lg;
                    pos = i;
                }
            }
        }
    }

    for (int i = 1; i <= n; ++ i) {
        ans += 1LL * a[i];
        ++ ans;
    }

    g << ans << '\n';
    return 0;
}