Advertisement
cska1312

06. Truck Tour

May 10th, 2023 (edited)
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <climits>
  3. #include <queue>
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. bool canFinishTour(queue<int> amounts,queue<int> distances)
  10. {
  11.   int reservoir = 0;
  12.   while(amounts.size())
  13.     {
  14.       reservoir += amounts.front();
  15.  
  16.       int distanceToNext = distances.front();
  17.       if(reservoir >= distanceToNext)
  18.       {
  19.         reservoir -= distanceToNext;
  20.       }else{
  21.         return false;
  22.       }
  23.      
  24.       distances.pop();
  25.       amounts.pop();
  26.     }
  27.   return true;
  28. }
  29.  
  30. int main()
  31. {
  32.   queue<int> distances;
  33.   queue<int> amounts;
  34.  
  35.   int numberOfStation;
  36.  
  37.   cin >> numberOfStation;
  38.  
  39.   for(int i = 0; i < numberOfStation; i++)
  40.     {
  41.       int buf;
  42.       cin >> buf;
  43.       amounts.push(buf);
  44.       cin >> buf;
  45.       distances.push(buf);
  46.     }
  47.   int curStationStart;
  48.   for(curStationStart = 0; curStationStart < numberOfStation; curStationStart++)
  49.     {
  50.       if(canFinishTour(amounts, distances))
  51.         break;
  52.  
  53.       int distance = distances.front();
  54.       int amount = amounts.front();
  55.       distances.pop(); amounts.pop();
  56.       distances.push(distance);
  57.       amounts.push(amount);
  58.     }
  59.      cout << curStationStart << endl;
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement