Cod sursa(job #1687689)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 13 aprilie 2016 00:32:04
Problema Numarare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

ifstream fin("numarare.in");
ofstream fout("numarare.out");

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

int main() {

	int n;
	fin >> n;

	for (int i = 1; i <= n; ++i)
		fin >> v[i];

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

	int right = 0, center = 0;
	for (int i = 1; i < n; ++i) {

		int i_mirror = 2 * center - i;
		if (right >= i)
			p[i] = min(right - i, p[i_mirror]);

		while (i - p[i] - 1 > 0 && i + p[i] + 1 < n && a[i - p[i] - 1] == a[i + p[i] + 1])
			++p[i];

		if (i + p[i] > right) {
			right = i + p[i];
			center = i;
		}

	}

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

		sol += p[i] + 1;

	}

	fout << sol << '\n';

	return 0;

}

//Trust me, I'm the Doctor!