#PS0000200. Phân biệt tiền tố đơn điệu (Testing Monotone Prefix Assumptions)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phân biệt tiền tố đơn điệu (Testing Monotone Prefix Assumptions)
Testing Monotone Prefix Assumptions
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers (possibly negative) and a positive integer , find the shortest nonempty consecutive interval with sum at least , or -1 if none exists. A student uses the following algorithm:
l = 1, sum = 0, best = n + 1
for r = 1 .. n:
sum = sum + a[r]
while l <= r and sum >= K:
best = min(best, r-l+1)
sum = sum - a[l]
l = l + 1
if best == n + 1: best = -1
This algorithm can fail on negative elements. Print the correct answer and the best value returned by the exact algorithm above, even if they are equal. Different values identify a counterexample.
Input
The first line contains . The second line contains exactly integers .
Output
Print two space-separated integers on one line: the correct answer and the result of the prescribed algorithm.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; .
Example 1
Input
2 4
-2 5
Output
1 -1
Explanation
Interval [2,2] sums to 5; the prescribed algorithm sees sums -2 and 3 and never shrinks its window.
Example 2
Input
3 4
2 1 3
Output
2 2
Explanation
Interval [2,3] sums to 4; the prescribed algorithm also finds length 2.