Pagini recente » Cod sursa (job #458124) | Cod sursa (job #1357264) | Cod sursa (job #1756128) | Cod sursa (job #1998748) | Cod sursa (job #31907)
Cod sursa(job #31907)
# include <stdio.h>
const int MAXV=30001; //30001
const int MAXN=500000; //500000;
long int restpoz[MAXV+1],restneg[MAXV+1];
int heap [MAXN+1];
int elem [MAXN+1];
long int heaplen,n,k;
int rise_heap(long int poz)
{
long int m,ok,aux;
while (poz/2)
{
m=poz/2;ok=0;
if (heap[poz]<heap[m])
{aux=heap[poz];heap[poz]=heap[m];heap[m]=aux;poz=m;ok=1;}
poz=m;
if (ok==0) return 0;
}
return 0;
}
int submerge_heap(long int poz)
{
long int min,aux;
while (poz*2<=heaplen)
{
min=poz;
if (heap[poz*2]<heap[poz]) min=poz*2;
if (poz*2+1<=heaplen&&heap[poz*2+1]<heap[min]) min=poz*2+1;
//interschimbarea
if (min!=poz)
{
aux=heap[poz];heap[poz]=heap[min];heap[min]=aux;
poz=min;
}
else return 0;
}
return 0;
}
void scrie (int sol, long int psol)
{
FILE *g=fopen("secventa.out","w");
fprintf(g,"%ld %ld %d\n",psol,psol+k-1,sol);
fclose(g);
}
int main()
{
int aux,sol,ok;long int psol,i,j;
FILE *f=fopen("secventa.in","r");
fscanf(f,"%d%d",&n,&k);
for (i=1;i<=k;i++)
{
fscanf(f,"%d",&aux);
elem[i]=aux;
heap[++heaplen]=aux;
rise_heap(heaplen);
}
j=0;
sol=heap[1];psol=1;
for (i=k+1;i<=n;i++)
{
j++; if (j>k) j=1;
fscanf(f,"%d",&aux);
if (elem[j]>=0) restpoz[elem[j]]++;
else restneg[-elem[j]]++;
//adauga aux in heap
heap[++heaplen]=aux;
elem[j]=aux;
rise_heap(heaplen);
//vezi daca trebuie sa scoti radacina cumva
if (heap[1]<0) if (restneg[-heap[1]]) ok=1;
else ok=0;
else if (restpoz[heap[1]]) ok=1;
else ok=0;
while (ok)
{
if (heap[1]>=0) restpoz[heap[1]]--;
else restneg[-heap[1]]--;
heap[1]=heap[heaplen];
heaplen--;
heap[heaplen+1]=0;
submerge_heap(1);
if (heap[1]<0) if (restneg[-heap[1]]) ok=1;
else ok=0;
else if (restpoz[heap[1]]) ok=1;
else ok=0;
}
if (sol<heap[1]) {sol=heap[1];psol=i-k+1;}
}
scrie(sol,psol);
fcloseall();
return 0;
}