Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <ranges>
- #include <algorithm>
- template <typename TCont>
- void print(const TCont& s, std::string delim = " ")
- {
- std::ranges::copy(s, std::ostream_iterator<typename TCont::value_type>(std::cout, delim.c_str()));
- std::cout << std::endl;
- }
- template <template <typename> class TCont, typename TType>
- TCont<TType> fill_from(int count, std::istream& from)
- {
- TCont<TType> seq;
- seq.reserve(count);
- std::ranges::copy(
- std::ranges::istream_view<TType>(from),
- std::inserter(seq, seq.end()));
- return seq;
- }
- size_t get_min_time_to_get_down(std::vector<size_t>& floors, size_t lift_capacity)
- {
- auto get_next_filled_floor_idx = [](const std::vector<size_t>& floors)
- {
- std::vector<size_t> next_filled_floor_idx;
- for (auto i = 0; i < floors.size(); ++i)
- {
- if (floors[i] != 0)
- {
- next_filled_floor_idx.emplace_back(i);
- }
- }
- return next_filled_floor_idx;
- };
- auto next_filled_floor_idx = get_next_filled_floor_idx(floors);
- size_t min_time_to_get_down = 0;
- for (auto i = 0; i < next_filled_floor_idx.size(); ++i)
- {
- auto filled_floor_idx = next_filled_floor_idx[i];
- if (floors[filled_floor_idx] < lift_capacity && i != next_filled_floor_idx.size() - 1)
- {
- floors[next_filled_floor_idx[i + 1]] += floors[filled_floor_idx];
- continue;
- }
- min_time_to_get_down += (floors[filled_floor_idx] / lift_capacity) * (filled_floor_idx + 1) * 2;
- if (size_t remaining_people = floors[filled_floor_idx] % lift_capacity; i == next_filled_floor_idx.size() - 1 && remaining_people != 0)
- {
- min_time_to_get_down += (filled_floor_idx + 1) * 2;
- }
- else if (remaining_people != 0)
- {
- floors[next_filled_floor_idx[i + 1]] += remaining_people;
- }
- }
- return min_time_to_get_down;
- }
- int main()
- {
- size_t lift_capacity, floor_cnt;
- std::cin >> lift_capacity >> floor_cnt;
- std::vector<size_t> floors = fill_from<std::vector, size_t>(floor_cnt, std::cin);
- std::cout << get_min_time_to_get_down(floors, lift_capacity) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement