#include #include #define HEAPSIZE 260000 int heaplen; int theap [HEAPSIZE]; void insert (int val) { int parent, child, temp; heaplen++; child = heaplen - 1; parent = child >> 1; theap [heaplen - 1] = val; while (child > 0) { if (theap [child] > theap [parent]) break; temp = theap [child]; theap [child] = theap [parent]; theap [parent] = temp; child = parent; parent = child >> 1; }; } void dispheap (void) { int i; printf ("heap: "); for (i = 0; i < heaplen; i++) printf ("%d ", theap [i]); printf ("\n"); } void delete (void) { int parent, child, temp; heaplen--; parent = 0; child = 2*parent + 1; theap [parent] = theap [heaplen]; while (child < heaplen) { if (child < heaplen - 1) if (theap [child+1] < theap [child]) child++; if (theap [parent] < theap [child]) break; temp = theap [child]; theap [child] = theap [parent]; theap [parent] = temp; parent = child; child = 2*parent + 1; }; } int main (void) { int taskcnt, tasknum; int tlen, tahead; int textpos; tasknum = 1; scanf ("%d", &taskcnt); while (taskcnt--) { printf ("Zadani %d:\n", tasknum); heaplen = textpos = 0; while (1) { scanf ("%d %d", &tlen, &tahead); if (!tlen && !tahead) break; /* dispheap (); */ printf ("%d\n", heaplen); insert (textpos + tlen + tahead); textpos += tlen; while (textpos >= theap [0] && heaplen > 0) delete(); /* dispheap (); */ }; printf ("\n"); tasknum++; }; return 0; }