Gadha_Kishan's blog

By Gadha_Kishan, history, 11 months ago, In English

although i am newbie ..... I am learning in every corner .... I am just helping others who are stuck in this problem for such time..

I am talking about ----Codeforces Round 811 (Div. 3) 1 no question-----

A. Everyone Loves to Sleep

weird and easy right I have not slept for 10 hours it was not easy for me for the first time when i saw that....somehow i found the logic within 20 minutes but somehow i didn't implemented it for any reason maybe for less practice ...But i knew how and what will be the answer ..then i tried tried.....for 7 hours fixing ....then i found out that i was looking for how many hours and minutes he had been sleeping but trust although my approach was right but there are some issues in edges cases ...then i saw some yt videos it didn't help that much ......

finally i saw @tourist god i clicked on his submit and saw his code it was damn short and math related....

then it took 3 hours more to just expolre his thought,tescases and logic .... finally i got that ...

//////////////////////////////////////// main things over here--------- \\\\\\\\\\\\\\\\\\\\

you guys will understand if you dive into it...

int diff = y — x;--------------------------------if you talking about sleep in same day and wakeup at also same day then just simply find out the minimum------

if (diff < 0) {-----but if you talking about sleep at 23.45  alarm at then this happend------
    diff += 1440;
  }
  ans = min(ans, diff);
  • Vote: I like it
  • -5
  • Vote: I do not like it

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

brooo thanks a lot you save my lots of time

»
11 months ago, # |
  Vote: I like it +9 Vote: I do not like it

I'm a newbie myself so others' advice probably takes precedence over mine,

Generally if you work out a small case you can find a general solution.

So if the alarm is 9:00 and you sleep 8:30, you know hours = 0 and minutes = 30. It appears as though it is just ending time minus starting time

But then lets say the alarm is 7:00 and I sleep 8:30. If I use the same logic as before, I get hours = -1 and minutes = -30, which does not work. So I have to consider it as the next day (7:00 becomes 31:00). Then the elapsed time works and I can do 31:00 — 8:30 = 22:30 slept.

But then lets say the alarm is at 8:15 and I sleep 8:30. For previous cases, it was obvious for the code to know what time comes before because the hour was different. But here, the hour is the same, so then our code has to compare the minutes.

Once you start thinking about these test cases then you can then code it up. I'd advise against jumping to the keyboard until you actually know what your algorithm is based on.

Competitive programming just works like that.

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Why do this much math..just simple start from the current hour and keep on incrementing minutes until you reach any of the alarm times..simple brute force

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

i think reading about the basics of modular arithmetic could help you