Pagini recente » simulare_oji_2023_clasele_11_12_11_martie | Cod sursa (job #573652) | Cod sursa (job #12869) | Cod sursa (job #2034138) | Cod sursa (job #486051)
Cod sursa(job #486051)
#include <cstdio>
#define MAXN 100010
using namespace std;
int N;
int v[MAXN];
int ext[MAXN];
int main(){
freopen("numarare.in","r",stdin);
freopen("numarare.out","w",stdout);
scanf("%d",&N);
int lval;
scanf("%d",&lval);
for (int i=1;i<N;++i){
scanf("%d",&v[i]);
int aux=v[i];
v[i]=lval-v[i];
lval=aux;
}
--N;
if (N==0){
printf("%d\n",0);
fclose(stdout);
return 0;
}
int npalis=1;
int highint=1;
int lowint=1;
for (int i=2;i<=N;++i)
if (i>highint){
int clow=i-1;
int chigh=i+1;
while (clow>=1 && chigh<=N && v[clow]==v[chigh]){
--clow;
++chigh;
}
++clow;
--chigh;
ext[i]=i-clow+1;
npalis+=ext[i];
if (chigh>highint){
highint=chigh;
lowint=clow;
}
} else {
int j=lowint-i+highint;
ext[i]=ext[j];
if (ext[i]>(highint-i+1)) ext[i]=highint-i+1;
int clow=i-ext[i];
int chigh=i+ext[i];
while (clow>=1 && chigh<=N && v[clow]==v[chigh]){
--clow;
++chigh;
}
++clow;
--chigh;
ext[i]=i-clow+1;
npalis+=ext[i];
if (chigh>highint){
highint=chigh;
lowint=clow;
}
}
printf("%d\n",npalis);
fclose(stdout);
return 0;
}