Advertisement
tamim_arefin_anik

Untitled

Dec 7th, 2021
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. /* Question
  2.  
  3. Brain Station-23 has a Robot! This Robot has a very special food habit. Dependending on a day of the week, It eats certain type of food:
  4.  
  5. on Monday, Thursday and Sunday It eats black circuit boards;
  6. on Tuesday and Saturday It eats red circuit boards;
  7. on other days of week he eats green circuit boards.
  8. BS-23 plans to go on an annual trip. This Robot takes a backpack which contains:
  9.  
  10. a daily rations of black circuit boards;
  11. b daily rations of red circuit boards;
  12. c daily rations of green circuit boards.
  13. BS-23 has to select such a day of the week to start that there robot can eat without additional food purchases as long as possible. If Bs-23 chooses the day of the week to start optimally, help them to find the maximum number of days the Robot can eat in a trip without additional food purchases.
  14.  
  15. Input
  16. The first line of the input contains three positive integers a, b and c — the number of daily rations of black circuit boards, red circuit boards and green circuit boards in Robot's backpack correspondingly.
  17.  
  18. 1 ≤ a,b,c ≤7 * 108
  19.  
  20. Output
  21. Print the maximum number of days the Robot can eat in a trip without additional food purchases, if BS-23 chooses the day of the week to start his trip optimally.
  22.  
  23. Examples
  24. Input
  25. 2 1 1
  26. Output
  27. 4
  28. Input
  29. 3 2 2
  30. Output
  31. 7
  32. Input
  33. 1 100 1
  34. Output
  35. 3
  36. Input
  37. 30 20 10
  38. Output
  39. 39
  40. Note
  41. In the first example the best day for start of the trip is Sunday. In this case, during Sunday and Monday the robot will eat black circuit board, during Tuesday — red circuit board and during Wednesday — green circuit board. So, after four days of the trip all food will be eaten.
  42.  
  43. /*
  44.  
  45.  
  46.  
  47. #include <bits/stdc++.h>
  48. using namespace std;
  49.  
  50. /*
  51. Complexity: O(log n)
  52. Idea: Binary search on number of day we can plan for trip.
  53. And check if it is possible of not to trip for this many day if we start from all 7 days.
  54. */
  55.  
  56. long long x,y,z;
  57. bool check(long long now)
  58. {
  59.     long long week = now/7;
  60.     bool flag = false;
  61.  
  62.     for(int i=0;i<7;i++)
  63.     {
  64.         long long b = x - week * 3, r = y - week * 2, g = z - week * 2;
  65.         int idx = i;
  66.         int remDay = now%7;
  67.         while(remDay--){
  68.             if(idx==0) r--;
  69.             else if(idx==1) b--;
  70.             else if(idx==2) b--;
  71.             else if(idx==3) r--;
  72.             else if(idx==4) g--;
  73.             else if(idx==5) b--;
  74.             else g--;
  75.  
  76.             idx = (idx+1)%7;
  77.         }
  78.  
  79.         if(b>=0 and r>=0 and g>=0) flag = true;
  80.     }
  81.     return flag;
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87.     cin >> x >> y >> z;
  88.    
  89.     long long lo = 0, hi = 1e18, ans = -1;
  90.     while(lo<=hi)
  91.     {
  92.         long long mid = (lo+hi)/2;
  93.         if(check(mid)){
  94.             ans = mid;
  95.             lo = mid + 1;
  96.         }
  97.         else hi = mid - 1;
  98.     }
  99.     cout << ans;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement