Hi, Darko.
I solved this problem just now.
I took one year to understand what my code was wrong.
My method is here.
At first, sort by their IQ in decreasing order.
If there are same IQ and different weights,
then sort by their weights in decreasing order.
Second, LIS by their weight.
In this case, LIS try to find n maximum lengths.
Each of the i-th maximum lengths contains i-th elephant.
Refer here -> Method to solve
http://www.comp.nus.edu.sg/~stevenha/pr ... _(LIS/LDS).
After this step, we can find the longest length of n maximum lengths by a single-loop. I call the longest length 'LL' for my explanation.
Finally, try to find the concrete order of elephants.
Make a single-loop (n to 1), and search each of the minimum weights of LL-th to 1-th longest length.
Although N to 1 loop can keep the sorted data consistency, 1 to N loop will failed.
Because of my English is poor, I'll explain by using sample input.
Initial data set :
Code: Select all
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
After sorting by IQ :
Code: Select all
1000 4000
1100 3000
6000 2100
6000 2000
500 2000
2000 1900
8000 1400
6008 1300
6000 1200
Result of LIS : 1 2 3 3 1 3 4 4 4
(This result is same form as 'Methods to Solve')
Then we can know the longest length LL is 4.
Make a single-loop (N to 1) and search. This case N = 9.
i = 9, min_weight(4) = min(infinity, 6000) =
6000.
i = 8, min_weight(4) = min(6000, 6008) = 6000.
i = 7, min_weight(4) = min(6000, 8000) = 6000.
i = 6, min_weight(3) = min(infinity, 2000) =
2000.
i = 5, skip because the min_weight(2) is not found yet.
i = 4, min_weight(3) = min(2000, 6000) = 2000.
i = 3, min_weight(3) = min(2000, 6000) = 2000.
i = 2, min_weight(2) = min(infinity, 1100) =
1100.
i = 1, min_weight(1) = min(infinity, 1000) =
1000.
Then we can get the answer.
1000 4000
1100 3000
2000 1900
6000 1200
If you want to know why your code get WrongAnswer,
please check by using the second input which I posted previous.
Your code may be able to find the longest length '55', but how about the elephants order?
Perhaps, although IQs are sorted, weights are an inconsistent order.
Thank you.