Editorial for WC '15 Contest 1 S1 - Woburn Workload
Submitting an official solution before solving the problem yourself is a bannable offence.
Given a bunch of due dates and durations, we must decide which assignments to focus on (and when) in order to minimize the penalty marks. Observe that penalty marks for any assignment is directly proportional to the time we spend not working on the assignment, regardless of which assignment is in question. As long as we spend the longest possible total time working, we spend the shortest possible total time not working, and in turn will incur the fewest possible total penalty marks.
Next, notice that since we can only work on one assignment at a time, we can greedily choose to work on assignments that will contribute the most time to our total working time. Intuitively knowing when a greedy algorithm works is a good skill to have for programming contests, since we don't have to rigorously prove anything to be able to implement it. A good indicator that a greedy algorithm will work is the fact that by being greedy with respect to one property, we are not sacrificing anything else. In this case, every assignment carries the same rate of penalty per unit of time (maximizing time worked and minimizing penalty go hand-in-hand), so we are losing nothing by simply being greedy in terms of maximizing time worked. So, the greedy algorithm we apply is as follows:
- Read the assignments one by one and keep a running variable storing the "position" we are on our timeline. Initialize this variable to time .
- At any point in time,
position
will store the point in time that we've just decided to stop working on the most recent assignment (either because we've worked on it for all minutes to produce the best work, or because we've gone past the due date and gave up, in which caseposition
will be equal to the due date). - Whenever an assignment comes up, start working on the assignment immediately from our current position. In other words, get on working through all of the new assignment's minutes immediately at the current time.
- However, it might not be possible that we can do it before the assignment's due date. So, if
position
is greater than the due date, we set it equal to the due date and calculate the penalty to add to the answer. - At this point, we are ready to handle the next assignment.
By studying the algorithm on some examples, you should be able to convince yourself that through this method, we're doing our absolute best to work on assignments for the longest possible time overall. If at any position
(we have nothing to do for the moment) we are given an assignment that we can work on for all minutes and still not go past its due date, we obviously should do it all. However, if we should decide between sticking to one assignment that we're already working on versus switching to a new assignment, we know that it's obviously better to move on to the most recent assignment that was inputted because the due date is later.
Comments