Pagini recente » Cod sursa (job #2962983) | Cod sursa (job #2137462) | Cod sursa (job #697366) | Cod sursa (job #1052037) | Cod sursa (job #1462805)
#include <stdio.h>
#include <string.h>
#define nmax 500005
#define SIZE 3500005
int n, k, baza, ind;
int a[nmax], deque[nmax], i, j;
int first = 1, last = 0;
char string[SIZE];
char *nr;
int main()
{
FILE *in, *out;
in = fopen("secventa.in", "r");
out = fopen("secventa.out", "w");
fscanf(in, "%d %d\n", &n, &k);
fgets(string, SIZE, in);
i = 1;
nr = strtok(string, " ");
while (nr != NULL)
{
j = (nr[0] == '-' ? 1 : 0);
while (nr[j] >= '0' && nr[j] <= '9')
a[i] = a[i]*10 + nr[j++] - '0';
a[i++] *= (nr[0] == '-' ? -1 : 1);
nr = strtok(NULL, " ");
}
for (i = 1; i <= n; i++)
{
while(first <= last && a[i] <= a[deque[last]])
last--;
deque[++last] = i;
if (i - deque[first] == k)
first++;
if (i >= k)
{
if (baza < a[deque[first]])
{
baza = a[deque[first]];
ind = deque[first];
}
}
}
while (a[ind-1] >= baza)
ind--;
fprintf(out, "%d %d %d\n", ind, ind + k-1, baza);
}