Cod sursa(job #486047)

Utilizator victor.ionescuIonescu Victor Cristian victor.ionescu Data 20 septembrie 2010 13:08:09
Problema Numarare Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <cstdio>
#define MAXN 200010
using namespace std;
int N;
int v[MAXN];
int ext[MAXN];

int main(){
	
	freopen("numarare.in","r",stdin);
	freopen("numarare.out","w",stdout);
	
	int L;
	
	scanf("%d",&L);
	N=0;
	
	for (int i=1;i<=L;++i){
		int x;
		scanf("%d",&x);
		v[++N]=x;
		if (i<L) v[++N]=-200000;
	}
	
	fclose(stdin);
	
	int lowint=1;
	int highint=1;
	ext[1]=1;

	int nsec=0;
	
	for (int i=2;i<=N;++i)
		
		if (i>highint){
			int clow=i-1;
			int chigh=i+1;
			
			int checks;
			if (v[i]==(-200000)) checks=v[i-1]+v[i+1]; else checks=v[i-2]+v[i+2];
			
			while (clow>=1 && chigh<=N && (v[clow]==-200000 || (v[clow]+v[chigh]==checks))){
				--clow;
				++chigh;
			}
			
			++clow;
			--chigh;
			
			ext[i]=i-clow+1;
			
			if (v[i]==(-200000)) nsec+=ext[i]/2;
			
			if (chigh>highint){
				highint=chigh;
				lowint=clow;
			}
		} else {
			
			int j=lowint+highint-i;
			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];
			
			int checks;
			if (v[i]==(-200000)) checks=v[i-1]+v[i+1]; else checks=v[i-2]+v[i+2];
			while (clow>=1 && chigh<=N && (v[clow]==-200000 || (v[clow]+v[chigh]==checks))){
				--clow;
				++chigh;
			}
			
			++clow;
			--chigh;
			
			ext[i]=i-clow+1;
			
			if (v[i]==(-200000)) nsec+=ext[i]/2;
			
			if (chigh>highint){
				highint=chigh;
				lowint=clow;
			}
		}
		
		
		
	printf("%d\n",nsec);
	
	fclose(stdout);
	
	return 0;
}