The Baric Bovine
InterviewTime limit1sMemory limit128 MB
Choose the smallest subset of N pressure readings so the total interpolation-style error stays within E, and report that size plus the least error for it.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
During one day Bessie takes atmospheric-pressure measurements and numbers them in the order she observes them (, ).
Bessie wants a subset of the measurements — indices () — that represents the whole set well, i.e. keeps the error defined below small.
Every index that is not in the subset contributes an error:
- if :
- if for some :
- if :
The total error is the sum of these individual errors. Given a maximum error (), find the size of the smallest subset whose total error is at most .
Input
- Line 1: two space-separated integers and .
- Lines 2 to : line contains the single integer .
Output
- Line 1: two space-separated integers — the size of the smallest subset whose total error is at most , and the least possible total error achievable with a subset of that size.
Hint
For example, with measurements , choosing the 2nd and 4th measurements is optimal and gives a total error of . The measurement before the first chosen index contributes , and the measurement between the two chosen indices contributes .