problem cf (1)

1
5.12(Udi Mamber) Let x1, x2, …, xn be a sequence of real numbers (not necessarily positive). Design an O(n) algorithm to find the subsequence xi, xi+1, …, xj (of consecutive elements) such that the product of the numbers in it is maximum over all consecutive subsequences. The product of the empty subsequence is defined by 1. Solution We have to begin with the naive induction hypothesis Induction hypothesis: We know how to find the solution for a sequence of size < n. when n = 1, then the maximum product subsequence consist of the single number if the number is more than 1, otherwise the empty subsequence. Consider a sequence S = (x1,x2, …, xn) of size n>1. By induction we know how to find the solution for S' = (x1,x2, …, xn-1). If the maximum subsequence is empty all the numbers in S' are less than 1, and we need to consider only xn. Assume that the maximum subsequence found by in S' is S'M = (xi,xi+1, …, xj) where 1<=i<=j<=n-1, the maximum subsequence of S have only two options: could be S'M or the maximum product suffix of S named Ssuffix_max , we know S'M, and we only need to know what is the value of Ssuffix_max, we have to strengthening our induction hypothesis for get S'suffix_max = (xk, xk+1, …, xn) too, if we have S'suffix_max we have to see how to find Ssuffix_max, if xn is positive then Ssuffix is S'suffix*xn if it is more than 1, otherwise the empty subsequence. Now what happen when xn is negative if we calculate with the same pointerview showed before the value will be negative, we need the minimum product suffix named Ssuffix_min, . We have to strengthening our hypothesis more getting S'suffix_min = (xl,xl+1, …, xn-1), we can calculate Ssuffix_max, it is S'suffix_min*xn if it is more than 1, or, 1 otherwise. If xn is 0 then Ssuffix_max is the empty subsequence. Now we can calculate the maximum product subsequence, it is the maximum of S'M and Ssuffix. Strength induction hypothesis: We know how to find for a sequence of size < n, the maximum product subsequence, the maximum product suffix and the minimum product suffix. To update Ssuffix_min and Ssuffix_max, if xn is positive Ssuffix_min = min(1, S'suffix_min*xn) and Ssuffix = max(1, S'suffix*xn), if xn is negative Ssuffix_min = min(1, S'suffix*xn) and Ssuffix = max(1, S'suffix_min*xn) and if xn is 0 Ssuffix_max = 1, Ssuffix_min = 1

Upload: elvis-capia-quispe

Post on 10-Jul-2016

212 views

Category:

Documents


0 download

DESCRIPTION

codeforces problem , go

TRANSCRIPT

Page 1: Problem CF (1)

5.12(Udi Mamber) Let x1, x2, …, xn be a sequence of real numbers (not necessarily positive). Design an O(n) algorithm to find the subsequence xi, xi+1, …, xj (of consecutive elements) such that the product of the numbers in it is maximum over all consecutive subsequences. The product ofthe empty subsequence is defined by 1.

Solution

We have to begin with the naive induction hypothesis

Induction hypothesis: We know how to find the solution for a sequence of size < n.

when n = 1, then the maximum product subsequence consist of the single number if the number is more than 1, otherwise the empty subsequence. Consider a sequence S = (x1,x2, …, xn) of size n>1. By induction we know how to find the solution for S' = (x1,x2, …, xn-1). If the maximum subsequence is empty all the numbers in S' are less than 1, and we need to consider only xn. Assume that the maximum subsequence found by in S' is S'M = (xi,xi+1, …, xj) where 1<=i<=j<=n-1, the maximum subsequence of S have only two options: could be S'M or the maximum product suffix of S named Ssuffix_max , we know S'M, and we only need to know what is the value of Ssuffix_max, we have to strengthening our induction hypothesis for get S'suffix_max= (xk, xk+1, …, xn) too, if we have S'suffix_max we have to see how to find Ssuffix_max, if xn is positive then Ssuffix is S'suffix*xn if it is more than 1, otherwise the empty subsequence. Now whathappen when xn is negative if we calculate with the same pointerview showed before the value will be negative, we need the minimum product suffix named Ssuffix_min, . We have to strengthening our hypothesis more getting S'suffix_min = (xl,xl+1, …, xn-1), we can calculate Ssuffix_max, it is S'suffix_min*xn if it is more than 1, or, 1 otherwise. If xn is 0 then Ssuffix_max is the empty subsequence. Now we can calculate the maximum product subsequence, it is the maximum of S'M and Ssuffix.

Strength induction hypothesis: We know how to find for a sequence of size < n, the maximum product subsequence, the maximum product suffix and the minimum product suffix.

To update Ssuffix_min and Ssuffix_max, if xn is positive Ssuffix_min = min(1, S'suffix_min*xn) and Ssuffix = max(1, S'suffix*xn), if xn is negative Ssuffix_min = min(1, S'suffix*xn) and Ssuffix =max(1, S'suffix_min*xn) and if xn is 0 Ssuffix_max = 1, Ssuffix_min = 1