10688  The Poor Giant
Moderator: Board moderators
 Krzysztof Duleba
 Guru
 Posts: 584
 Joined: Thu Jun 19, 2003 3:48 am
 Location: Sanok, Poland
 Contact:
Re: faster algorithm?
I did a DP solution with memoization which is roughly n^2 * k, but it's independent of the number of test cases (since answers can be reused). It took 1.1 secs, would have been a bit too slow for AC in the original contest, but it would probably be fast enough if the computations are done bottomup instead of with memoization.Maniac wrote:I've solved this problem with C++ using DP and my algorithm is of order n^3. My solution took about 5 seconds, but I see a lot of much faster solutions. Can anyone hint me about a faster solution?
The table I used was simply a twodimensional array indexed by n and k. Tell me if you want a bigger hint than that.
Thanks, I tried the solution you suggested and got AC in 1.203s with a bottomup implementation. So I think it isn't much better than the solution with memorization.
Isn't 1 sec a little on the low side for a time limit then? Prefactors really start to make the difference between AC and TLE in this case, which in my humble opinion should always be avoided...
Isn't 1 sec a little on the low side for a time limit then? Prefactors really start to make the difference between AC and TLE in this case, which in my humble opinion should always be avoided...
 Krzysztof Duleba
 Guru
 Posts: 584
 Joined: Thu Jun 19, 2003 3:48 am
 Location: Sanok, Poland
 Contact:

 New poster
 Posts: 4
 Joined: Sun Aug 29, 2004 3:46 pm
Wow, it has been a while (1 year+?) and you're still keep tap on this, that's great!
Well, from my understanding, you have a bunch of apples of weight k+1...k+n.
Now, you want to eat some apples in order and identify the sweet apple (exaclt one of that). Any apple with less weight than the sweet one is bitter and any apple with more weight is sour, as a result, suppose your apples have weight in the range [i,j], if you eat some apple of weight x, i<=x<=j, then you will know (1) the weet apple is x and you found it, (2) apple x is bitter and the actual sweet apple is thus in range [x+1,j], (3) apple x is sour and the actual sweet apple is in range[i,x1].
The problem with the problem statement is you don't know where the apple is exactly, and that's not what we're after. You want a cost function that is the sum of all the cost considering all possible locations of the sweet apple. So, basically you need to come up with a decision tree of eating apples such that if apple A k+1<=A<=k+n is the sweet apple, that decision tree will give you cost(A), and you want the decision tree such that Sum(cost(x), x=k+1..k+n) is minimized.
Heh, I think this is even more confusing then the problem statement...lol
So, basically in general you have a bunch of apples with weight in the range [i,j], and you eat some apple x. You then need to calculate the cost of eat x given range[i,j], and split the problem into [i,x1] and [x+1,j].
This is a typical type of DP where you store a range (i,j) of your problem.
Similar problems are: Matrix Chain Multiplication, Minimum Perimeter Triangulation, Bitonic TSP, Cutting Stick (some problem in vol 1), and a lot of others.
Learn this and you can get +50 in problems solved
Well, from my understanding, you have a bunch of apples of weight k+1...k+n.
Now, you want to eat some apples in order and identify the sweet apple (exaclt one of that). Any apple with less weight than the sweet one is bitter and any apple with more weight is sour, as a result, suppose your apples have weight in the range [i,j], if you eat some apple of weight x, i<=x<=j, then you will know (1) the weet apple is x and you found it, (2) apple x is bitter and the actual sweet apple is thus in range [x+1,j], (3) apple x is sour and the actual sweet apple is in range[i,x1].
The problem with the problem statement is you don't know where the apple is exactly, and that's not what we're after. You want a cost function that is the sum of all the cost considering all possible locations of the sweet apple. So, basically you need to come up with a decision tree of eating apples such that if apple A k+1<=A<=k+n is the sweet apple, that decision tree will give you cost(A), and you want the decision tree such that Sum(cost(x), x=k+1..k+n) is minimized.
Heh, I think this is even more confusing then the problem statement...lol
So, basically in general you have a bunch of apples with weight in the range [i,j], and you eat some apple x. You then need to calculate the cost of eat x given range[i,j], and split the problem into [i,x1] and [x+1,j].
This is a typical type of DP where you store a range (i,j) of your problem.
Similar problems are: Matrix Chain Multiplication, Minimum Perimeter Triangulation, Bitonic TSP, Cutting Stick (some problem in vol 1), and a lot of others.
Learn this and you can get +50 in problems solved
Re: 10688  The Poor Giant
Is there a way to prevent those spams?

 New poster
 Posts: 37
 Joined: Wed Mar 14, 2012 11:57 am
 Location: Bangladesh
 Contact:
Re: 10688  The Poor Giant
Seems like the problem statement has not been fixed yet. I was so confused seeing this:
I kept on checking my code over and over again, until I realized, the equation is just plain wrong. 1+3+3+3 = 10, not 14. Can't believe it took me so long to see the arithmetic error1+3+3+3=13