Advertisement
updown

Untitled

Mar 23rd, 2023
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. ll last_cow = arr[0].first;
  2.     ll interval = 0;
  3.     for (int i=1; i<N; i++) {
  4.         // First try placing the cow in the current interval
  5.         ll lowest_pos_new_loc = last_cow + val;
  6.  
  7.         if (lowest_pos_new_loc <= arr[interval].second) {
  8.             last_cow = lowest_pos_new_loc;
  9.             continue;
  10.         }
  11.  
  12.         // Need to find the next interval to place the cow
  13.         while (interval < M && arr[interval].second > lowest_pos_new_loc) {
  14.             interval ++;
  15.         }
  16.  
  17.         // Case 1: we ran out of intervals
  18.         if (interval == M) return false;
  19.  
  20.         last_cow = max(lowest_pos_new_loc, arr[interval].first);
  21.  
  22.     }
  23.     return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement