Pagini recente » Cod sursa (job #2651612) | Cod sursa (job #429892) | Cod sursa (job #1503086) | Cod sursa (job #1499185) | Cod sursa (job #44955)
Cod sursa(job #44955)
#include <stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define NMAX 100010
#define INF 666000666
int V[NMAX], N, K, Kk, Dmin[NMAX], Dmax[NMAX], DifMax, Pmin[NMAX], Pmax[NMAX];
int main()
{
int i, lomax=0, himax=1, lomin=0, himin=1;
freopen("vila2.in", "r", stdin);
scanf("%d %d", &N, &K);
if (K>N) K = N;
for (i = 1; i <= N; i++) scanf("%d", V+i);
K = 2*K;
Dmin[0] = INF;
for (i = 1; i <= K; i++)
{
while (V[i]>Dmax[himax-1] && himax>0) himax--;
Dmax[himax]=V[i]; Pmax[himax++]=i;
while (V[i]<Dmin[himin-1] && himin>0) himin--;
Dmin[himin]=V[i]; Pmin[himin++]=i;
}
DifMax = Dmax[0]-Dmin[0];
for (i = K+1; i <= N; i++)
{
if (i-Pmin[lomin]>K) lomin++;
if (i-Pmax[lomax]>K) lomax++;
while (V[i]>Dmax[himax-1] && himax>lomax) himax--;
Dmax[himax]=V[i]; Pmax[himax++]=i;
while (V[i]<Dmin[himin-1] && himin>lomin) himin--;
Dmin[himin]=V[i]; Pmin[himin++]=i;
DifMax = MAX(DifMax, Dmax[lomax]-Dmin[lomin]);
}
freopen("vila2.out", "w", stdout);
printf("%d\n", DifMax);
return 0;
}