Cod sursa(job #3279368)

Utilizator tomavladnicolae@gmail.comTomavlad [email protected] Data 22 februarie 2025 18:26:42
Problema Numarare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("numarare.in");
ofstream fout("numarare.out");
int n, a[100005], s[100005], cnt[100005];
long long sol;

int main()
{
    int i, j, st, dr, l, r;
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> a[i];
    for (i = 2; i <= n; i++)
        s[i - 1] = a[i] - a[i - 1];
    n--;
    l = r = 1;
    s[0] = 2e9; s[n + 1] = -2e9;
    for (i = 1; i <= n; i++)
    {
        cnt[i] = max(0, min(r - i, cnt[l + (r - i)]));
        while (s[i - cnt[i]] == s[i + cnt[i]])
            cnt[i]++;
        if (i + cnt[i] > r)
        {
            l = i - cnt[i];
            r = i + cnt[i];
        }
    }
    for (i = 1; i <= n; i++)
        sol += cnt[i];
    fout << sol << "\n";
    fout.close();
    return 0;
}