Updated: Thank everyone for your helps, but it seems that I missed a certain condition of the problem, and my version maybe really just impossible to solve :v, the original problem with that condition is a thousand time easier than this and is straight-forward to CP coders like us (The original problem description was a bit confusing to me :'( )
Hi everyone,
today, I am stuck with a problem. I hope you can help me with it. I think this is a very interesting problem and I am not even sure there exists a deterministic solution to it. The problem is as follows:
" Given n (n <= 1000, but O(n^3) is also fine) distinct integers, group them into sets of at least 4 elements, and each set is an Arithmetic Sequences. "
Example:
Input: 1250, 3748, 6246, 8744, 2493, 4993, 7493, 9993, 2497, 4996, 7495, 9994, 2498, 4997, 7496, 9995, 2499, 4998, 7497, 9996
Output: (1250, 3748, 6246, 8744), (2493, 4993, 7493, 9993), (2497, 4996, 7495, 9994), (2498, 4997, 7496, 9995), (2499, 4998, 7497, 9996)
In this test, if you find the longest sequence first, then the rest will not form any valid sequence anymore.
(Assumption: For the given input, there always exists at least one solution that satisfies the constraints and no number is left)
I have spent hours working on this problem but unfortunately I cannot think of any ideas that will work well in general. What do you think?
Thank you.
What are the bounds on the distinct integers?
There is no clear bound, but it should be from 0 to 10000.
Does the order matter?
No, it does not matter. The only constraint is that each set is an Arithmetic Sequence of size at least 4.
Can you check the correctness of the following algorithm and the computational complexity of each step:
Example:
Let n = 8, and a = { 12, 5, 9, 4, 7, 16, 8, 3 };
The sorted array is a = { 3, 4, 5, 7, 8, 9, 12, 16 };
The valid entries of b are:
key = 1, value = { (0,1), (1,2), (3,4), (4,5) }
key = 2, value = { (0,2), (2,3), (3,5) }
key = 3, value = { (1,3), (2,4), (5,6) }
key = 4, value = { (0,3), (1,4), (2,5), (4,6), (6,7) }
key = 5, value = { (0,4), (1,5), (3,6) }
Maximal-length chains:
key = 2: { 0, 2, 3, 5 }
key = 4: { 1, 4, 6, 7 }
The final solution contains two Arithmetic Sequences:
{ 3, 5, 7, 9 }
{ 4, 8, 12, 16 }
If each distinct integer should not appear in more than one sequence, then a maximal-length chain may not exist in the final solution if one of its members u exists in another chain of a different valid entry.
A refined algorithm for generating valid entries and candidate sequences with improved computational complexity may exist.
Hope that helps.
It is a greedy, right? May you tried the above example? In my example, if you find the maximum chain that the remaining may not work anymore.
I have just added a note about maximal-length chains. Yes, the final solution may contain only a subset of each maximal-length chain if some distinct integer exists in more than one maximal chain in different valid map entries. In the example above, the maximal chains are disjoint. So, the final solution contains both chains.
The following is the C++17 code used to compute the valid entries in the example above:
what is the answer for this test case? 10 1 2 4 8 16 32 64 128 256 612
This test is not possible, because 612 (the biggest) — 256 (the second biggest) = 356 > any other number, which means it cannot belongs to any arithmetic sequence of length 4. So I guess the answer for it is not valid. (I did not mention, but let's assume that there always exists a partition for the input)
Appending an extra integer 22 to this test case makes it valid with only one possible arithmetic sequence:
4 10 16 22
What is the source of this problem?
It was in a private event (not really CP, but rather software engineering, the tests were weak) in my city yesterday, so it is not possible to access it now, but after removing all the crazy engineering part, this is the hardest that I am clueless.
Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
This might not really answer the question. But...
In the contest yesterday (I competed in Vienna), it was also possible that some numbers were actually not part in any sequence. The problem statement therefore was not really well defined, and it leaves room for multiple solutions. Since a number can be part in multiple arithmetic sequences. And it was not clear which sequence should we prefer. E.g. [1250, 2499, 3748, 4997, 6246, 7495, 8744, 9993] would also be a fine solution according to the problem statements.
So I just assumed that there is only one unique answer. Otherwise the statement would had explain how to handle multiple solutions. I just took every pair of numbers a and b, and checked if there is a sequence a, a + d, a + 2 * d, ... with d = b - a. If it was, then I removed it and continued searching. Complexity was .
Thank you, your assumption is actually correct and your answer is indeed the solution. I have reread the problem and seems that I missed a certain condition that the sequence will cover the whole [start, end].
(All the time I thought they were having a checker like Codeforces :'( , cannot believe that I was almost stuck at that one, what a shame for me as a CP coder :'( ! )
No worries. It is quite common in these contests that they are hard to understand and it is often hard to find all hidden information. This year was the first time that I did pretty good (2nd place in Vienna with 5 completed levels). In the last years I always performed way worse, because I also struggled a lot with some problem statements.
Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).