Under construction

Still being built. Things may change

Paranoid Cows

Interview

Time limit1sMemory limit1024 MB

Summary
Given N time intervals, find the largest K such that the first K intervals contain no pair where one strictly contains another.
Level

Medium5 of 10

Topics
Sorting, Binary search
Solved
No attempts yet

Paranoid Cows

Farmer John has NN cows (1≤N≤100,0001 \le N \le 100{,}000) who are self-conscious about their milk output. A cow who does not produce much milk is likely to be teased by the other cows.

FJ has set a milking schedule in which each cow ii (1≤i≤N1 \le i \le N) is assigned a time interval [Ai,Bi][A_i, B_i] (0≤Ai<Bi≤1,000,000,0000 \le A_i < B_i \le 1{,}000{,}000{,}000). Cow ii enters the barn at time AiA_i to be milked and leaves at time BiB_i. The barn door is narrow and fits only one cow at a time, so FJ made sure that no two cows enter or leave at the same time.

If the interval of cow ii contains the interval of cow jj, that is, Ai<Aj<Bj<BiA_i < A_j < B_j < B_i, the two intervals nest. Nesting is bad. Cow ii stays in the barn for the whole of cow jj's milking and can spy on cow jj to learn her milk output. Cows do not want others to know their output, so FJ wants the schedule to contain no nested intervals.

Help FJ find the largest index KK (1≤K≤N1 \le K \le N) such that the intervals of cows 11 through KK contain no nesting.

Examples1

  1. Example 1

    Input
    5
    7 20
    1 4
    3 12
    6 10
    0 3
    
    Expected output
    3