Created
February 9, 2020 10:47
-
-
Save LostInKadath/b777df1690c9509d1ad1fcaf7e9430b4 to your computer and use it in GitHub Desktop.
Task 207. Lineland
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <thread> | |
#include <vector> | |
std::ostream& operator<<(std::ostream& os, const std::vector<int>& v) | |
{ | |
os << "["; | |
for (const auto& i : v) | |
os << i << ' '; | |
return os << "]"; | |
} | |
std::vector<int> Task207(const std::vector<int>& costs) | |
{ | |
const size_t sz = costs.size(); | |
if (0 == sz) | |
return {}; | |
std::vector<int> result(sz, -1); | |
std::vector<std::thread> threads(sz - 1); | |
for (size_t i = 0; i < sz - 1; ++i) | |
{ | |
threads[i] = std::thread([&result, &costs, sz, i] | |
{ | |
const auto currentCost = costs[i]; | |
for (size_t j = i; j < sz; ++j) | |
{ | |
if (costs[j] < currentCost) | |
{ | |
result[i] = j; | |
break; | |
} | |
} | |
}); | |
} | |
for (auto& thread : threads) | |
thread.join(); | |
return result; | |
} | |
int main() | |
{ | |
for (auto i = 0; i < 10; ++i) | |
{ | |
const std::vector<int> costs = { 1, 2, 3, 2, 1, 4, 2, 5, 3, 1 }; | |
const auto result = Task207(costs); | |
std::cout << costs << "\n\t" << result << "\n"; | |
} | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment