#include #include #define HEAPSIZE 260000 int heaplen; int theap [HEAPSIZE]; void insert (int val) { int parent, child, temp; child = heaplen; parent = (child - 1) >> 1; theap [heaplen] = val; heaplen++; while (child > 0) { if (theap [child] > theap [parent]) break; temp = theap [child]; theap [child] = theap [parent]; theap [parent] = temp; child = parent; parent = (child - 1) >> 1; }; } void delete (void) { int parent, child, temp; heaplen--; parent = 0; child = (parent << 1) + 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 = (parent << 1) + 1; }; } void dispheap (void) { int i; printf ("heap: "); for (i = 0; i < heaplen; i++) printf ("%d ", theap [i]); printf ("\n"); } 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 ((theap [0] <= textpos) && (heaplen > 0)) delete(); /* dispheap (); */ }; printf ("\n"); tasknum++; }; return 0; }