Pagini recente » Cod sursa (job #1653919) | Cod sursa (job #638952) | Cod sursa (job #2796984) | Cod sursa (job #402709) | Cod sursa (job #1462803)
#include <stdio.h>
#include <string.h>
#define nmax 500005
#define SIZE 3500005
int n, k, baza, index;
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]];
index = deque[first];
}
}
}
while (a[index-1] > baza)
index--;
fprintf(out, "%d %d %d\n", index, index + k-1, baza);
}