Cod sursa(job #636228)

Utilizator mihaipopa12Popa Mihai mihaipopa12 Data 19 noiembrie 2011 17:52:31
Problema Zombie Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 1.67 kb
#include<stdio.h>

#define maxn 1000005

FILE*f=fopen("zombie.in","r");
FILE*g=fopen("zombie.out","w");

int d,n,k,i,pmin,st,x,L,p,m,u,lenght,ch;
char D[maxn],Poz[maxn],H[maxn],v[maxn];
char buff[7000000];

inline void swap ( char &a , char &b ){
	int aux = a; a = b; b = aux;
}

inline void urca(int poz){
	while ( poz != 0 && D[ H[poz/2] ] > D[ H[poz] ] ){
		swap(H[poz],H[poz/2]);
		swap(Poz[H[poz]],Poz[H[poz/2]]);
		poz = poz >> 1;
	}
}

inline void coboara(int x){
	int y = 0;
	
	while ( x != y ){
		y = x;
		if ( (y << 1) <= L && D[H[ y << 1 ]] < D[H[ x ]] )
			x = y << 1;
		if ( (y << 1) + 1 <= L && D[H[(y<<1)+1]] < D[H[ x ]] )
			x = ( y << 1 ) + 1;
		swap(H[x],H[y]);
		swap(Poz[H[x]],Poz[H[y]]);
	}
}

inline void insert_heap ( int x) {
	H[++L] = x; Poz[x] = L;
	urca(L);
}

inline void erase_first () {
	swap(H[1],H[L]); swap(Poz[H[1]],Poz[H[L]]); H[L] = 0; Poz[H[L]] = -1;
	--L; coboara(1);
}

inline int cb ( int val ){
	p = 1; u = i-1;
	
	while ( p <= u ){
		m = p + (u - p) / 2;
		if ( v[m] >= val )
			u = m - 1;
		else
			p = m + 1;
	}
	return p;
}

inline int next () {
	int r = 0;
	while ( buff[ch] >= '0' && buff[ch] <= '9' && ch <= lenght ){
		r = r * 10 + buff[ch] - '0';
		++ch;
	}
	++ch;
	return r;
}

int main () {
	
	//fscanf(f,"%d %d %d",&d,&n,&k); 
	lenght = fread(buff+1,1,6999997,f); ch = 1;
	d = next(); n = next(); k = next();
	st = -k + 1;
	for ( i = 1 ; i <= n ; ++i ){
		++st; if ( st > 0 )	insert_heap(st);
		x = next(); v[i] = x;
		D[i] = D[i-1] + 1;
		pmin = cb(x-d);
		while ( H[1] < pmin && L ){
			erase_first();
		}
		if ( D[i] > D[H[1]-1] + k ){
			D[i] = D[H[1]-1] + k;
		}
	}
	
	fprintf(g,"%d\n",D[n]);
	
	fclose(f);
	fclose(g);
	
	return 0;
}