Cod sursa(job #2466289)

Utilizator Carol_LucaCarol Luca Carol_Luca Data 1 octombrie 2019 20:32:35
Problema Numarare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>



using namespace std;



ifstream fin ("numarare.in");

ofstream fout ("numarare.out");



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

int r, mij;



int main()  {

  fin >> n;

  for (int i = 1; i <= n; ++i)  {

    fin >> a[i];

  }

  for (int i = 1; i < n; ++i)

    a[i] = a[i + 1] - a[i];

  --n;

  int ans = 0;

  for (int i = 1; i <= n; ++i)  {

    if (i > r)  {

      int x = 1;

      while (a[i + x] == a[i - x] && i + x <= n && i - x >= 1)

        ++x;

      --x;

      r = i + x;

      mij = i;

      ans += x + 1;

      v[i] = x;

    }

    else  {

      int x = min(v[2 * mij - i], r - i);

      while (a[i + x] == a[i - x] && i + x <= n && i - x >= 1)

        ++x;

      --x;

      if (i + x > r)  {

        r = i + x;

        mij = i;

      }

      ans += x + 1;

      v[i] = x;

    }

  }

  fout << ans;

  return 0;

}