After getting TLE for my first couple of tries, I have found a problem case for time limit problem. If you are trying to solve this brute force, you might want to use this data to test your code.
This one took around 1 minute on my P4-2.2GHz with brute force. Making input even longer, up to 30 boxes using same pattern, makes it much more time consuming (no I have not enough patience to time it ).
After a slight optimization, same input pattern with 30 boxes took less than 1sec to solve.
I has solved this problem 103 before . But due to recorrection it got a WA.
My algo is simple ..
I define a order of the boxes and sort them. Then I apply the longest increasing
subsequence algo to get the correct sequence.
The order in which I sort is if Box A can be placed inside Box B the A shud come
before in the order . It is topological sorting but I do it just with a simple sort routine.
and maybe say what the correction was.
also, i first sort dimensions of boxes for each box, since they can be turned on any side, thus i find out if box A fits into box B.
how did u do?
the rest of the algo is ok, same as my AC
Understanding a problem in a natural way will lead to a natural solution
I have a problem with my solution.
Here's what my program does:
1. reads the input and stores it into an array
2. It will put the numbers increasingly in the same array:
if the input is
--- /*start of input*/
1 6
1 8 3 2 10 20
--- /*end of input*/
So:
1 2 3 8 10 20
then it will compare all the boxes and store the data of which box is bigger than which box.
then the "which box is bigger looks like this:
it means, that
1 /*box number 1 is the smallest*/
2 7 /*box number 2 is bigger than box nr. 8*/
3 1 2 7 /*3 is bigger than 1 2 and 7*/
4 7 /* and so on*/
5 1 2 7
6 1 2 4 5 7
7
8 1 2 5 7
the it will call a recursive function which finds the longest possible string.
My source code is about 200 lines(I commented it alot) and I think it is no point to examine it.
I want to know, if there is a logical error in my logic or not.
What does that DP abbreviation mean and how does that algorithm work.
Now if I understood the problem correctly, I have to rearrange the measurments of 1 box so that it will fit into a bigger box, which measurements are bigger than the box, whose measurements I changed.
As I can see, you sort the boxes increasingly
1 2 2 4
1 2 3 4
1 2 4 4
And then I should check all the array DATA [1..3]
so that if DATA[2] has a side, which is <= with one of DATA[1] sides, then I will eliminate DATA[2]. I do the same with all boxes which meet the condition.
And In the end, I just output the remaining boxes indexes and the number of those indexes in the front.
True?
One problem with your code is that you don't compile with warnings enabled and/or don't read them. The format strings you used for scanf are wrong. The correct format string for a signed short is %hd, not %d. On a 32-bit platform in gcc, long is the same as int, unsigned long == unsigned int and the correct format string is %u, not %d.
As a side note, there is really no need to use signed shorts instead of ints... there is plenty of memory available, ints enable you to store larger numbers and they are even a bit faster.