Cod sursa(job #3277351)

Utilizator mateilucaLuca Matei Gabriel mateiluca Data 15 februarie 2025 20:37:50
Problema Numarare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 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;
}