Advertisement
fenixD3

The lift

Nov 23rd, 2023
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ranges>
  4. #include <algorithm>
  5.  
  6. template <typename TCont>
  7. void print(const TCont& s, std::string delim = " ")
  8. {
  9.     std::ranges::copy(s, std::ostream_iterator<typename TCont::value_type>(std::cout, delim.c_str()));
  10.     std::cout << std::endl;
  11. }
  12.  
  13. template <template <typename> class TCont, typename TType>
  14. TCont<TType> fill_from(int count, std::istream& from)
  15. {
  16.     TCont<TType> seq;
  17.     seq.reserve(count);
  18.  
  19.     std::ranges::copy(
  20.         std::ranges::istream_view<TType>(from),
  21.         std::inserter(seq, seq.end()));
  22.  
  23.     return seq;
  24. }
  25.  
  26. size_t get_min_time_to_get_down(std::vector<size_t>& floors, size_t lift_capacity)
  27. {
  28.     auto get_next_filled_floor_idx = [](const std::vector<size_t>& floors)
  29.     {
  30.         std::vector<size_t> next_filled_floor_idx;
  31.         for (auto i = 0; i < floors.size(); ++i)
  32.         {
  33.             if (floors[i] != 0)
  34.             {
  35.                 next_filled_floor_idx.emplace_back(i);
  36.             }
  37.         }
  38.  
  39.         return next_filled_floor_idx;
  40.     };
  41.  
  42.     auto next_filled_floor_idx = get_next_filled_floor_idx(floors);
  43.  
  44.     size_t min_time_to_get_down = 0;
  45.     for (auto i = 0; i < next_filled_floor_idx.size(); ++i)
  46.     {
  47.         auto filled_floor_idx = next_filled_floor_idx[i];
  48.         if (floors[filled_floor_idx] < lift_capacity && i != next_filled_floor_idx.size() - 1)
  49.         {
  50.             floors[next_filled_floor_idx[i + 1]] += floors[filled_floor_idx];
  51.             continue;
  52.         }
  53.  
  54.         min_time_to_get_down += (floors[filled_floor_idx] / lift_capacity) * (filled_floor_idx + 1) * 2;
  55.  
  56.         if (size_t remaining_people = floors[filled_floor_idx] % lift_capacity; i == next_filled_floor_idx.size() - 1 && remaining_people != 0)
  57.         {
  58.             min_time_to_get_down += (filled_floor_idx + 1) * 2;
  59.         }
  60.         else if (remaining_people != 0)
  61.         {
  62.             floors[next_filled_floor_idx[i + 1]] += remaining_people;
  63.         }
  64.     }
  65.     return min_time_to_get_down;
  66. }
  67.  
  68. int main()
  69. {
  70.     size_t lift_capacity, floor_cnt;
  71.     std::cin >> lift_capacity >> floor_cnt;
  72.  
  73.     std::vector<size_t> floors = fill_from<std::vector, size_t>(floor_cnt, std::cin);
  74.  
  75.     std::cout << get_min_time_to_get_down(floors, lift_capacity) << std::endl;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement