Код:
#include
#include
#include
#include
#include
#include
std::vector get_number_sequence(const int N, const std::string& title_message) {
std::vector sequence(N);
std::cout << title_message << std::endl;</p>
for(auto i = 0; i < N; ++i) {
int number;
std::cin >> number;
sequence[i] = number;
}
return sequence;
}
template std::multimap
zip(std::vector first_sequence, std::vector second_sequence) {
std::multimap store;
if (first_sequence.size() != second_sequence.size()) {
throw std::runtime_error("Argument exception. It can't zip vectors with different sizes");
}
for (auto i = 0; i < first_sequence.size(); ++i) {
store.insert(std::pair (first_sequence[i], second_sequence[i]));
}
return store;
}
template std::multimap
reversed_zip(std::vector sequence) {
std::multimap store;
for (auto i = 0; i < sequence.size(); ++i) {
store.insert(std::pair (sequence[i], i+1));
}
return store;
}
template std::multimap zip(std::vector sequence) {
std::multimap store;
for (auto i = 0; i < sequence.size(); ++i) {
store.insert(std::pair (i+1, sequence[i]));
}
return store;
}
template std::pair, std::vector>
unzip(std::multimap store) {
std::vector keys;
std::vector values;
for (auto it = store.begin(); it != store.end(); ++it) {
keys.push_back(it->first);
values.push_back(it->second);
}
return std::pair, std::vector>(keys, values);
}
template std::pair, std::vector>
reversed_unzip(std::multimap store) {
std::vector keys;
std::vector values;
for (auto it = store.begin(); it != store.end(); ++it) {
keys.push_back(it->second);
values.push_back(it->first);
}
return std::pair, std::vector>(keys, values);
}
template
void print_values(std::vector sequence) {
std::cout << sequence[0];</p>
for (auto i = 1; i < sequence.size(); ++i) {
std::cout << " " << sequence[i];</p>
}
std::cout << std::endl;</p>
}
int main() {
auto NT = get_number_sequence(2, "Enter the N and T values > ");
auto N = NT[0];
auto T = NT[1];
auto pigs = reversed_zip(get_number_sequence(N, "Enter N pigs > "));
auto cities = get_number_sequence(N, "Enter N cities > ");
auto cent = get_number_sequence(N, "Enter N cents > ");
auto calculated_cities = std::vector(N);
for(auto i = 0; i < N; ++i) {
calculated_cities[i] = cent[i] - cities[i] * T;
}
auto stored_cities = reversed_zip(calculated_cities);
auto sorted_pigs = reversed_unzip(pigs).first;
auto sorted_cities = reversed_unzip(stored_cities).first;
auto sorted_values = zip(sorted_cities, sorted_pigs);
auto processed_values = unzip(sorted_values).second;
std::cout << "Answer is ";</p>
print_values(processed_values);
return 0;
}