fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int carFleet(int target, vector<int> position, vector<int> speed) {
  5. vector<pair<int,int>>nums;
  6. for(int i=0;i<position.size();i++) {
  7. nums.push_back({position[i],speed[i]});
  8. }
  9. sort(nums.begin(), nums.end());
  10.  
  11.  
  12. stack<pair<int,int>>st;
  13.  
  14. for(int i=0; i<nums.size();i++) {
  15. while(!st.empty() && st.top().second>nums[i].second) {
  16. // if(st.top().first + st.top().second <= target) st.pop();
  17. int pos1 = st.top().first;
  18. int speed1 = st.top().second;
  19. int pos2 = nums[i].first;
  20. int speed2 = nums[i].second;
  21.  
  22. double value = std::abs(speed1 * pos2 - speed2 * pos1) / std::abs(speed1 - speed2);
  23. int roundedUp = static_cast<int>(ceil(value));
  24. cout << fixed << setprecision(5) << value << endl;
  25.  
  26. if(roundedUp<=target) st.pop();
  27. else break;
  28. }
  29. st.push(nums[i]);
  30. }
  31. return st.size();
  32. }
  33.  
  34. int main() {
  35. // your code goes here
  36. cout<<carFleet(13, {10,2,5,7,4,6,11}, {7,5,10,5,9,4,1});
  37. return 0;
  38. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
6.00000
7.00000
22.00000
11.00000
12.00000
12.00000
13.00000
1