Tallest Cow
InterviewTime limit1sMemory limit128 MB
Given the tallest cow's height and index plus pairs where cow a sees cow b, find each cow's maximum possible height consistent with all observations.
- Level
Medium5 of 10
- Topics
- Greedy, Prefix sum, Hash map, Implementation
- Solved
- No attempts yet
Problem
Farmer John's cows (), numbered through , are standing in a line. Every cow has a positive integer height, most of which are secret. You are told only the height () of the tallest cow and its index .
You are also given pieces of information () of the form "cow sees cow ". This means cow is at least as tall as cow , and every cow strictly between and is strictly shorter than cow .
For each cow from to , determine its maximum possible height so that all of the given information still holds. It is guaranteed that all constraints can be satisfied simultaneously.
Input
- Line 1: Four space-separated integers , , , and .
- Lines 2 to : Two distinct space-separated integers and (), meaning cow sees cow .
Output
- Print lines. Line contains the maximum possible height of cow .