Код:
#include <iostream>
#include <utility>
#include <vector>
#include <map>
#include <string>
#include <exception>
std::vector<int> get_number_sequence(const int N, const std::string& title_message) {
std::vector<int> sequence(N);
std::cout << title_message << std::endl;
for(auto i = 0; i < N; ++i) {
int number;
std::cin >> number;
sequence[i] = number;
}
return sequence;
template <typename TKey, typename TValue> std::multimap<TKey, TValue>
zip(std::vector<TKey> first_sequence, std::vector<TValue> second_sequence) {
std::multimap<TKey, TValue> 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<TKey, TValue> (first_sequence[i], second_sequence[i]));
return store;
template <typename T> std::multimap<T, int>
reversed_zip(std::vector<T> sequence) {
std::multimap<T, int> store;
for (auto i = 0; i < sequence.size(); ++i) {
store.insert(std::pair<T, int> (sequence[i], i+1));
template <typename T> std::multimap<int, T> zip(std::vector<T> sequence) {
std::multimap<int, T> store;
store.insert(std::pair<int, T> (i+1, sequence[i]));
template <typename TKey, typename TValue> std::pair<std::vector<TKey>, std::vector<TValue>>
unzip(std::multimap<TKey, TValue> store) {
std::vector<TKey> keys;
std::vector<TValue> 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<TKey>, std::vector<TValue>>(keys, values);
reversed_unzip(std::multimap<TKey, TValue> store) {
keys.push_back(it->second);
values.push_back(it->first);
template <typename T>
void print_values(std::vector<T> sequence) {
std::cout << sequence[0];
for (auto i = 1; i < sequence.size(); ++i) {
std::cout << " " << sequence[i];
std::cout << std::endl;
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<int>(N);
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 ";
print_values(processed_values);
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Код:
#include <iostream>
#include <utility>
#include <vector>
#include <map>
#include <string>
#include <exception>
std::vector<int> get_number_sequence(const int N, const std::string& title_message) {
std::vector<int> sequence(N);
std::cout << title_message << std::endl;
for(auto i = 0; i < N; ++i) {
int number;
std::cin >> number;
sequence[i] = number;
}
return sequence;
}
template <typename TKey, typename TValue> std::multimap<TKey, TValue>
zip(std::vector<TKey> first_sequence, std::vector<TValue> second_sequence) {
std::multimap<TKey, TValue> 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<TKey, TValue> (first_sequence[i], second_sequence[i]));
}
return store;
}
template <typename T> std::multimap<T, int>
reversed_zip(std::vector<T> sequence) {
std::multimap<T, int> store;
for (auto i = 0; i < sequence.size(); ++i) {
store.insert(std::pair<T, int> (sequence[i], i+1));
}
return store;
}
template <typename T> std::multimap<int, T> zip(std::vector<T> sequence) {
std::multimap<int, T> store;
for (auto i = 0; i < sequence.size(); ++i) {
store.insert(std::pair<int, T> (i+1, sequence[i]));
}
return store;
}
template <typename TKey, typename TValue> std::pair<std::vector<TKey>, std::vector<TValue>>
unzip(std::multimap<TKey, TValue> store) {
std::vector<TKey> keys;
std::vector<TValue> 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<TKey>, std::vector<TValue>>(keys, values);
}
template <typename TKey, typename TValue> std::pair<std::vector<TKey>, std::vector<TValue>>
reversed_unzip(std::multimap<TKey, TValue> store) {
std::vector<TKey> keys;
std::vector<TValue> 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<TKey>, std::vector<TValue>>(keys, values);
}
template <typename T>
void print_values(std::vector<T> sequence) {
std::cout << sequence[0];
for (auto i = 1; i < sequence.size(); ++i) {
std::cout << " " << sequence[i];
}
std::cout << std::endl;
}
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<int>(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 ";
print_values(processed_values);
return 0;
}