Problem Link:Difficulty:Simple Prerequisites:Dynamic Programming Problem:Given an array D[1...N], find max(abs((D[i] + ... + D[j])  (D[k] + ... D[l])) : 1<= i <= j < k <= l <= N). Explanation:Let us look at the final solution, and work backwards. Let the final solution be due to some i0, j0, k0, l0. We now have two cases: Case 1: D[k0] + ... + D[l0] <= D[i0] + ... + D[j0]. Case 2: D[k0] + ... + D[l0] > D[i0 + ... + D[j0]. Hence, it would be useful precomputing values that answer "what is the [minimummaximum] value I can get if I [startend] at position i?" Solution 1The above setting motivates the following few definitions: Recurrences for the above are easy to find: Note that, using the above recurrences, we can calculate all the arrays in O(N) time using dynamic programming. Finally, our case (1) will be covered by SoftLeftMax(j0)  HardRightMin(j0+1), and case (2) will be covered by HardRightMax(j0+1)  SoftLeftMin(j0). Solution 2This solution cleverly disposes of SoftLeftMin, SoftLeftMax() functions and works relying on the following claim. Claim: Without loss of generality, k0  j0 = 1. That is, we can consider our optimal phases as being consecutive. Let us say that OPT returned i, j, k, l, with kj > 1. Now, consider sum S = D[j+1] + D[j+2] + ... + D[k1]. If S >= 0, then it can be added to the larger of the two segments [i...j], [k...l]. If S <= 0, then the segment can be added to the smaller of the two segments [i...j], [k...l]. In both cases, it gives us a Delish value atleast as good as the Optimal. Hence, wlog, the two segments we choose are consecutive. Thus, finally, we iterate over j, and consider abs(LeftMax(j)  RightMin(j+1)) and abs(RightMax(j+1)  LeftMin(j)) as candidates. This approach was used by the Setter. Setter's Solution:Can be found here Tester's Solution:Can be found here
This question is marked "community wiki".
asked 18 Jun '13, 00:19

Can anyone explain me this solution of karlheinz_jung???? I have tried my best to understand it, but unfortunately I can't. Those who solved this question help me in figuring a case where my solution went wrong. answered 18 Jun '13, 02:03
really.. that annoying.. coding right.. it's better to understand someother code(esp. above tutorial) than that purely written code..
(18 Jun '13, 02:13)
1
haha.... I knew it bro.... I was jst thinking, in what world karlheinz would be, when he wrote the code.... I mean how a person can write such a code.... I think he himself cant xplain that....
(18 Jun '13, 02:41)
1
dafaq is this code !!!whre's a main() ??
(18 Jun '13, 11:29)
@spandanpathak, It has a main.. but nothing else is understandable. Probably we first need to replace all those variable to simple ones and the format it to understand where the functions start and end.. :P
(18 Jun '13, 12:00)
1
That code gives me urges that he must have done something which he is hiding behind that dirty code :)
(18 Jun '13, 12:09)
@shashank_jain: I always prefer akash4983's code for this purpose . least use of templates , least use of all the big constructs and more of a simple oriented approach devoid of problems associated with using abs , fabs , mods etc.
(18 Jun '13, 13:03)
1
The code seems to be the output of a decompiler or obfuscator. Some reasons which come to my mind are either he would have done it to optimise the code (eg, compile with gcc O3 and then decompile) or maybe he cheated from someone else so to make the code look different obfuscated it.
(18 Jun '13, 15:37)
2
I guess he used some translation tool (between programming languages) or other. After doing below conversion(and formatting a little), the code will be fine. Though the basic idea is the same as mine, his implementation is simpler. I fun to work out this puzzle :) (i_d6>readingBuff),(i_d8>readInt), (i_d12,i_d13,i_d14,i_d15: nonuse), (i_d9>sign),(i_d10>value),(i_d11>ch), (i_d7>curChPtr),(i_d18>testCase), (i_d19>i),(i_d20>N),(i_d22>D),(i_d21>maxV), (i_d23>sumMaxL),(i_d24>sumMaxR),(i_d25>sumMinL),(i_d26>sumMinR), (i_d27>pLL),(i_d28>endD),("\45\154\154\144\n">"%lld\n")
(22 Jun '13, 18:24)
showing 5 of 8
show all

So, finally, after reading the editorials several times and after searching for some AC solutions, I finally got AC with my solution in C++:
Thanks for this enlightening editorial @pragrame, which allowed me to write what I believe to be a clean solution for this problem! Keep the good work up :D I hope I can also devote some of my time to understand the problem Wstring a bit better and hopefully code a solution for it as soon as my time allows me to do so! I hope I can really improve something with this good contest :D Also, @xpertcoder, Thanks for your words! As you probably noticed, those problems are easy ones and those are the concepts which I can say that I feel most comfortable with, so, as you can see I still have a loong way to go :) Best regards, Bruno answered 18 Jun '13, 15:47
4
Nice work, I've also seen some of your tutorials and one time you even explained a problem to me. You have really nice logic, I think if you don't give up you'll become a really strong competitor and be able to fight for the first spots. Also, I've seen you complain a lot about math basis but you shouldn't let it stop you from solving problems. If you do that you'll just be wasting your talent, we should turn our weaknesses into our greatest strengths. In no way I'm saying this to only encourage you, I say this because it's true.
(18 Jun '13, 15:57)
Thanks @junior94, I hope that if I stick around this community my knowledge can grow much, much more and I can grow as a coder and as a person as well :D Everyone's words are a great motivating factor and hopefully things will go better on next month or at least as good as they went on this one :)
(18 Jun '13, 16:03)
Your solution is very neat but with an observation you could dove done it a lot cleaner. Think about reversing the array after finding the frist min and max.
(18 Jun '13, 18:40)
Maybe then it's possible to use only 2 arrays instead of four?
(18 Jun '13, 19:18)
I was aiming to the fact that you could've used the first two methods for the other calculations aswell,
(19 Jun '13, 00:57)

As contests pass by... and as I see I keep struggling to solve only 3 maybe 4 problems over 10 days... The more convinced I get that I might be too dumb for doing Algorithms at the minimum acceptable competitive level... After reading editorial I believe I understood the underlying idea behind DP a bit better (Truth is, I say this, but, as I never, or rarely get the chance to practice properly, I always end up only reading the editorial and not coding anything with my own comments etc, etc, which is what I think is needed to gain more skill). Nontheless, I found it absolutely hillarious that my code almost had the same designations as the Solution 2 described in editorial... However, I got stuck with the code EXACTLY like this for all the contest, after solving 3 problems in the 1st day of contest... I can't express how frustrated I feel with myself right now at this moment... I feel like FUCKTHISSHIT I really hope I can improve something... If I can't, this is just... very demotivating... :( answered 18 Jun '13, 02:28
same story...... here :/
(18 Jun '13, 02:32)
4
think it this way.... Compare your present knowledge with the knowledge you had when you started competitive coding..... we all know that it must be way better than at that time.... Now you are a part of a dedicated community whose members are not only working for themselves but are working for each other.... At least you are better than your mates who dont even knw a bit about competitive programming.... At least we try our best to solve these tuff problems and become part of a big competitions... Cheerup bro these conditions will reoccur.... all what we need is unstoppable efforts... ;)
(18 Jun '13, 03:03)
1
Well, @devanshug, you're right and it is indeed a lot better now than it was before... I even got to write some Simple problems as a setter and I actually think I managed to write good tutorials here on forums... But sometimes I feel that this is all I got.. Maybe it's the lack of time that makes me think this way, but it's still very frustrating and demotivating... I think I will try to work as hard as university allows me, in order to get a little bit better though :) Thanks for your words
(18 Jun '13, 03:10)
2
@kuruma, Being relatively new to competitive programming and algorithms besides the constraints due to university, i feel the same as i have not been able to solve more than 3 problems but each time we get stuck we learn something new(be it the simplest of things) but that is what should drive us as these little things go a long way in enhancing our programming skills. Bit by Bit, Bytes of knowledge can be acquired, learnt and applied to improve our speed and efficiency in solving more problems. And considering that you are a lot better than before, its an achievement in itself. Chin up!
(18 Jun '13, 11:30)
2
It's natural to feel this way maybe you were trying too hard...but as @devanshug said compare your knowledge now with what you have when you started...Key is to keep practicing...and C'mon you are the one who wrote questions like FCBARCA and CLBMSTRS which I loved to solve...these are way too creative..simple series questions hidden behind very creatively created stories...AWESOME MAN AWESOME!!! Happy Coding:)
(18 Jun '13, 13:29)

@pragrame  shouldn't we also include abs(LeftMax(j)RightMax(j+1)) and abs(LeftMin(j)RightMin(j+1)) as well because we are considering abs value ? If LeftMin(j)=2 and RightMax(j+1)=4 and RightMin(j+1)=10, then the max. abs value among these is abs(LeftMin(j)RightMin(j+1)) which is not checked! I apologise if I am missing some point ! Thanks ! answered 18 Jun '13, 00:39
2
Not really. In your example, you say LeftMin(j)  RightMin(j+1) is larger. But note that LeftMin(j) <= LeftMax(j). So your case will be considered (and potentially bettered) in the check for LeftMax(j)  RightMin(j+1). You can check that the same will happen with comparing other pairs  wherever you compare difference of "max with max" or "min with min".
(18 Jun '13, 01:30)

I made 2 functions using Kadane's algorithm for max and min. Function max gave me i,j,sum1. Function min gave me k,l,sum2. I took four cases. 1)max(0,arr.length2) and min(j+1,arr.length1) result1=Math.abs(sum1sum2). 2)min(0,arr.length2) and max(l+1,arr.length1) result2=Math.abs(sum1sum2). 3)max(1,arr.length1) and min(0,i1) result3=Math.abs(sum1sum2). 4)min(1,arr.length1) and max(0,k1) result4=Math.abs(sum1sum2). I took maximum of all four results.I satisfied all given cases as well as cases in comments still I got WA always any help or case where it fails please :( :(..... answered 18 Jun '13, 16:48

Cannot figure out what's wrong...Please Help.. http://www.codechef.com/viewsolution/2279041 answered 18 Jun '13, 21:46

@sid4art i did the same thing and got WA! http://www.codechef.com/viewsolution/2276586 Can someone please point out what is wrong with this approach? answered 19 Jun '13, 14:26

http://www.codechef.com/viewsolution/2299047 Can anyone tell whats wrong in this code???? answered 27 Jun '13, 17:04

By same reasoning , one can implement Kadane algorithm too. answered 24 Jun '15, 23:24

Can anyone explain me the output for 5 10 3 1 2 9 The output according to setter's or tester's code is 12. How? answered 14 Oct '17, 16:43

@dsahapersonal assuming t=1,n=5,d={10,3,1,2,9}, that is the correct answer ! We need to basically find two contiguous subarrays (no intersecting point) such that the absolute difference(if the diff is negative,it turns positive) between the sum of each subarray is maximum .Now,the main thing to note here is that the length of the subarray could also be 1. Therefore , if you consider these two subarrays : {10,3} and {1} then the difference is absolute(1(10+3))=absolute(12) = 12 ! Also if you take any other pair of subarrays ,you will find that the absolute difference is not greater than that. answered 02 Jun '18, 18:09

The sixth function is wrongly written. It should be leftmost not rightmost. HardRightMax(i) = Maximum value of the sum of a contiguous subarray whose leftmost point = i. answered 12 Sep '18, 18:44

The above solution provided in tutorial is giving tle while submitting in the java and the same solution give ac while submitting in java why? Java Solution>https://www.codechef.com/viewsolution/21872923 C++ Solution>https://www.codechef.com/viewsolution/21872897 answered 11 Dec '18, 17:08
