Cod sursa(job #2461981)

Utilizator Iulia25Hosu Iulia Iulia25 Data 26 septembrie 2019 17:12:42
Problema Numarare Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 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], x - 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;
}