Editorial for Wesley's Anger Contest 3 Problem 5 - Triangle: The Root of All Evil
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For subtask 1, checking all subsets of rods will suffice, taking the most optimal and valid subset. To check if a subset is valid simply check all triplets of the remaining rods and verify that the triangle inequality does not hold.
An optional optimization is to notice that the easiest way to break the triangle inequality is to find the two largest rods present that is less than the
Time Complexity:
Subtask 2
For subtask 2, we can build on the idea of the optional optimization using dynamic programming. Let
Time Complexity:
Subtask 3
For subtask 3, we can go for a greedy approach. In order to keep as many rods as possible, it is optimal to keep the ones with the smallest length, as this allows a greater range of possible rods to keep.
Time Complexity:
Subtask 4
For the full solution, we can observe that with two rods we have the range of possible rods we could keep based on the triangle inequality. As the rods we loop through increase, this range increases as well. To solve for an entry in the table, rather than loop through this range we can keep track of the best solution in the range using a two-dimensional prefix max array. To maintain this range we can keep an array of integers pointing to the endpoint of the range and update it with two-pointers.
Time Complexity:
Comments