Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <unordered_set>
  4. #include <thread>
  5. #include <future>
  6.  
  7. int foo(long long a, long long b, const std::unordered_set<long long>& set)
  8. {
  9. int result(0);
  10.  
  11. for (auto i = a; i < b; ++i)
  12. {
  13. for (auto&& n : set)
  14. {
  15. if (set.find(i - n) != set.end())
  16. {
  17. result++;
  18. break;
  19. }
  20. }
  21. }
  22. std::cout << result << std::endl;
  23. return result;
  24. }
  25.  
  26. int main()
  27. {
  28. auto thread_cnt = std::thread::hardware_concurrency();
  29. std::ifstream input_file("D:\\Playground\\2sum.txt");
  30. long long t;
  31. std::unordered_set<long long> set;
  32. while (input_file >> t)
  33. {
  34. set.insert(t);
  35. }
  36. auto m = -10000, n = 10000;
  37. std::vector<std::future<int>> fut;
  38. int d = (n - m) / thread_cnt;
  39. for (auto i = 0; i < thread_cnt; i++)
  40. {
  41. fut.push_back(std::async(foo, m, m + d, set));
  42. std::cout << m << " " << m + d << std::endl;
  43. m += d;
  44. }
  45. int r = 0;
  46. for (auto&& f:fut)
  47. {
  48. r += f.get();
  49. }
  50.  
  51. for (auto&& x : set)
  52. {
  53. if (set.find(n - x) != set.end())
  54. {
  55. r++;
  56. break;
  57. }
  58. }
  59. return r;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement