307  Sticks
Moderator: Board moderators
307: Sticks (NP Complete ?)
After getting a "memory exceeded" I ask myself:
Is this Problem NP Complete, and is my heuristic simply not good enough
or am I to stupid to see the trick.
At the moment I basically use dynamic programming. (I try to iterativly
build up the original sticks from the stick fragments, and store the solutions
of the already checked combinations.)
It would be nice to know the maximum number of stick fragments which
are used in the (secret) input. (This way it would be much easier to generate
realistic sample data for checking my code...)
so long
lundril
Is this Problem NP Complete, and is my heuristic simply not good enough
or am I to stupid to see the trick.
At the moment I basically use dynamic programming. (I try to iterativly
build up the original sticks from the stick fragments, and store the solutions
of the already checked combinations.)
It would be nice to know the maximum number of stick fragments which
are used in the (secret) input. (This way it would be much easier to generate
realistic sample data for checking my code...)
so long
lundril

 Guru
 Posts: 724
 Joined: Wed Dec 19, 2001 2:00 am
 Location: Germany
My solution worked with an array with 100000 elements. So there are no more than 100000 parts in the input.
But how do you store the used solutions? I think this is your problem. I used another array of 100000 elements to memorize, which parts I had used and reset it after every step (that means after I had not found a solution with length n and had to look for a solution with length n+1).
But how do you store the used solutions? I think this is your problem. I used another array of 100000 elements to memorize, which parts I had used and reset it after every step (that means after I had not found a solution with length n and had to look for a solution with length n+1).

 Guru
 Posts: 724
 Joined: Wed Dec 19, 2001 2:00 am
 Location: Germany
Any tips for problem 307?
Nothing else to say...it's just that I have been working on this thing for some time...no success till now...trying to use recursion to solve this problem.Any suggestions?
input needed
Hi,
I'm in dire need of some discriminative input. My program works for all cases I can think of, but I'm still getting WAs. I will not publish my code because I think (I hope) it would be a spoiler.
Many thanks in advance,
xenon
I'm in dire need of some discriminative input. My program works for all cases I can think of, but I'm still getting WAs. I will not publish my code because I think (I hope) it would be a spoiler.
Many thanks in advance,
xenon
WA 307 sticks
Can anybody help me what the flaw of algorithm I used in this problem?
1.read in all sticks, store them in a array and sort them in a decreasing order according to their length.
2.when reading in the sticks, I also use a variable to record the maximum length stick and sum of all sticks length.
3.starting from the maximum length, I use a example to describ:
Sample input
7
2 4 6 5 1 10 11
my algorithm
read in>sort(11 10 6 5 4 2 1)>check
sum of all element = 39.so we only need to check 13,39(because 1 and 3 to smaller than maximum element(11))
> first we check 13.
search the array from the begin index,so we can have (11,2).mark 11 and 2 as used element(when next search we should skip them) and then keep search.when we select 10,we can not find a unselected number to make their sum = 13,so break, answer is not be found yet and increse the maximum length,reset all sticks to unused.
>second we check 39,following the rule above, we can find (11,10,6,5,4,2,1),so answer is found.
1.read in all sticks, store them in a array and sort them in a decreasing order according to their length.
2.when reading in the sticks, I also use a variable to record the maximum length stick and sum of all sticks length.
3.starting from the maximum length, I use a example to describ:
Sample input
7
2 4 6 5 1 10 11
my algorithm
read in>sort(11 10 6 5 4 2 1)>check
sum of all element = 39.so we only need to check 13,39(because 1 and 3 to smaller than maximum element(11))
> first we check 13.
search the array from the begin index,so we can have (11,2).mark 11 and 2 as used element(when next search we should skip them) and then keep search.when we select 10,we can not find a unselected number to make their sum = 13,so break, answer is not be found yet and increse the maximum length,reset all sticks to unused.
>second we check 39,following the rule above, we can find (11,10,6,5,4,2,1),so answer is found.
That looks all well and good. It should at least in theory provide the correct answer, but it might be too slow... you need to optimize the backtracking quite a lot to not get TLE!
Also, I'm not sure how it is now, but earlier there were sticks >50 in the input data (even though the input says there are no such sticks) and those sticks you should just ignore.
The following input
Also, I'm not sure how it is now, but earlier there were sticks >50 in the input data (even though the input says there are no such sticks) and those sticks you should just ignore.
The following input
should yield48
3 7 8 4 1 1 6 3 6 1 5 5 8 4 8 2 7 6 3 3 8 5 6 7 4 8 6 6 8 5 8 3 3 5 5 4 1 8 3 1 2 7 1 6 2 6 8 8
38
2 6 6 8 7 4 1 8 4 1 4 4 3 3 2 3 3 4 6 8 8 7 2 4 1 1 5 8 4 7 6 5 1 3 3 3 1 6
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
9
2 2 2 2 2 2 2 3 3
40
1 1 2 2 2 2 2 2 3 3 3 4 4 4 4 5 6 7 7 7 8 8 8 9 9 11 11 11 12 12 12 13 14 14 15 16 21 22 35 40
0
47
18
6
5
10
62
According to your input data, my program can produce the right answer.
And I also add some code to ignore the number that > 50 but still got Wrong Answer! . I haven't started to optimize my efficiency of my program but at least there is some simple input case that I still don't consider in my program will cause my program to produce the wrong answer.
And I also add some code to ignore the number that > 50 but still got Wrong Answer! . I haven't started to optimize my efficiency of my program but at least there is some simple input case that I still don't consider in my program will cause my program to produce the wrong answer.

 Guru
 Posts: 724
 Joined: Wed Dec 19, 2001 2:00 am
 Location: Germany

 Guru
 Posts: 724
 Joined: Wed Dec 19, 2001 2:00 am
 Location: Germany
You probably use the same algorithm as I did before the problem was rejudged. Take the first fitting part and if with recursion it can be put together to form a stick of the wanted length, don't backtrack this step. With the original data set it worked, but now they have added some difficult inputs.