#include #include struct node { /* Vrchol BVS */ int key; /* Hodnota prvku posloupnosti */ int pos; /* Index v posloupnosti */ struct item *f; /* Ukazatel do seznamu */ /* ... Ostatní tree--dependent věci */ }; struct item { /* Prvek seznamu */ struct node *n; /* Ukazatel na vrchol stromu */ struct item *prev, *next; /* Předchozí, další ve spojáku */ }; struct node *root; /* Kořen stromu */ struct item *last; /* Poslední (nejnovější) prvek */ struct node *insert(struct node *T, int x, int pos, int *new) { /* Pokud ve stromu T není prvek s 'key' = 'x', pak jej vloží a vrací 'new'=1, jinak vrací 'new'=0 */ } void delete(struct node *T, struct node *n) { /* Ze stromu 'T' smaže vrchol 'n' */ } struct node *find_bad(struct node *T, int x, int smer) { /* Ve stromu 'T' najde prvek s 'key' (i) větší než 'x' pro 'smer'=1 (ii) menší než 'x' pro 'smer'=0. Pokud takový prvek neexistuje, vrací NULL. */ } int find_min(struct node *T) { /* ... */ } int find_max(struct node *T) { /* ... */ } int D; /* Maximální diference */ int a, b; /* Infimum a supremum */ int pocet; /* Počet prvků aktuálního úseku */ void update(int smer) { struct node *p; struct item *i, *j; for (;;) { p = find_bad(root, smer ? a + D : b - D, smer); /* Najdeme nevyhovující prvek... */ if (!p) break; /* ...a rušíme všechny starší v aktuálním úseku */ i = p->f; if (i->next) i->next->prev = NULL; while (i) { delete(root, i->n); /* Smažeme vrchol */ j = i->prev; free(i); /* i prvek seznamu */ i = j; pocet--; /* Ubyl nám jeden prvek */ } } /* Nakonec ještě aktualizace druhé meze */ if (smer) b = find_max(root); else a = find_min(root); } void add(int x, int pos) { struct node *p; struct item *i; int new; p = insert(root, x, pos, &new); if (!new) { p->pos = pos; i = p->f; if (i->prev) i->prev->next = i->next; /* vyjmutí */ if (i->next) i->next->prev = i->prev; free(i); } i = (struct item *)malloc(sizeof(struct item)); i->next = NULL; i->prev = last; i->n = p; if (last) last->next = i; last = i; p->f = i; pocet++; } int main(void) { int N, x, pos; int kon, maxpocet; scanf("%d %d", &D, &N); kon = 0; maxpocet = 1; pos = 0; scanf("%d", &x); N--; a = b = x; add(x, 0); while (N--) { pos++; scanf("%d", &x); if (x < a) { a = x; update(1); } else if (x > b) { b = x; update(0); } add(x, pos); if (pocet > maxpocet) { kon = pos; maxpocet = pocet; } } printf("Krajni indexy nejdelsiho stabilniho useku jsou %d, %d.\n", kon - maxpocet + 2, kon + 1); return 0; }