I am doing the ladder for Div. 2 B on codeforces, and just did the 31st question.
http://codeforces.com/problemset/problem/4/B
I have been trying this simplw problem since morning but couldn’t wrap my head around it. Just couldn’t. Then I finally looked at the tutorial and used it to understand how to solve the problem. I feel bad that I saw the problem, I can’t keep doing this.
I have done this for a few more problem in the past, and i feel bad when i see how easy the solution is. If only I had though harder.
I need to do full justice to every problem I’m solving.
Dunno why i’m not able to solve problems today. They are not really any hard. I should be able to solve them. There;s so much to do! How will i ever be able to just pass easy problems in one single go? It shouldn’t take more than 20 mins max to solve these problems.
I just want to write here how i solved this particular problem eventually. I could just copy paste the tutorial here, but then i wouldn;t be able to learn anything!
Algorithm:-
1. Take the sum of all the lower bounds (minTime) as sm1 and the upper bounds (maxTime) as sm2, and check if sumTime lies in the range sm1<=sumTime<=sm2.
2. If not, then just print NO and return, else print YES and move ahead.
3. Declare a vector to keep the schedule<int> of each day for Peter’s study hours. Initialise it with the lower bound (minTime) for every day. Take another vector upper<int>, to save the upper bounds (maxTime) for every day.
4. So basically these many hours have been done by Peter already from the whole quota of sumTime. Hence, need to sumTime – sm1, these many hours still need to be given the the subject.
4.1. Start looping ove the array schedule for its whole length.
5. Calculate delta = min(need, upper[i] – schedule[i]).
6. Update schedule[i] +=delta; and need -=delta;
7. You can break when need=0, but that’s not totally necessary.
So basically, what we are doing is, we assigned to the schedule of each day, the minTime set by Peter’s parents for that day. Then we see how many hours of study are left in total. Now for the schedule of every day, we give it the minimum of either the full need which is left and the total no of hours for that day (maxTime – minTime).
I’m spending so much time to write this stupid blog post, because i feel bad of having looked at the algorithm and then coding it.
A contest is about to begin on codeforces in about 2 hours. I hope i do well and am able to do atleast 2 questions.
-jigsaw.