Pagini recente » Cod sursa (job #2318515) | Cod sursa (job #582158) | Cod sursa (job #2074385) | Cod sursa (job #1134370) | Cod sursa (job #753601)
Cod sursa(job #753601)
#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;
}