Cod sursa(job #753601)

Utilizator ChallengeMurtaza Alexandru Challenge Data 30 mai 2012 08:13:05
Problema Numarare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>

using namespace std;

const char InFile[]="numarare.in";
const char OutFile[]="numarare.out";
const int MaxN=100111;

ifstream fin(InFile);
ofstream fout(OutFile);

int N,V[MaxN],P[MaxN];
long long sol;

int main()
{
	fin>>N;
	for(register int i=1;i<=N;++i)
	{
		fin>>V[i];
	}
	fin.close();
	
	for(register int i=1;i<N;++i)
	{
		V[i]-=V[i+1];
	}
	V[N]=0;
	
	int stmax=0;
	int drmax=0;
	for(register int i=1;i<N;++i)
	{
		if(drmax<i)
		{
			stmax=drmax=i;
			while(1<=stmax-1 && drmax+1<N && V[stmax-1]==V[drmax+1])
			{
				++P[i];
				--stmax;
				++drmax;
			}
		}
		else
		{
			P[i]=min(P[stmax+drmax-i],drmax-i);
			stmax=i-P[i];
			drmax=i+P[i];
			while(1<=stmax-1 && drmax+1<N && V[stmax-1]==V[drmax+1])
			{
				++P[i];
				--stmax;
				++drmax;
			}
		}
		sol+=P[i]+1;
	}
	
	fout<<sol;
	fout.close();
	return 0;
}