Advertisement
Guest User

post_56922793_speedtest

a guest
Jul 7th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <random>
  5. #include <chrono>
  6. #include <utility>
  7.  
  8. class hiddenVector {
  9.  private:
  10.   std::vector<int> a;
  11.  public:
  12.   hiddenVector (int N) {
  13.     a.resize (N);
  14.     // make all them different, except two equal elements
  15.     for (int i = 0; i < N; i++)
  16.       a[i] = (i % 2 ? i : -i);
  17.     a[1] = 0;
  18.     // shuffle all
  19.     auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  20.     std::shuffle (a.begin(), a.end(), std::default_random_engine(seed));
  21.   }
  22.   int getSize() const {
  23.     return a.size();
  24.   }
  25.   bool iLessThanj(int i, int j) const {
  26.     return a[i] < a[j];
  27.   }
  28. };
  29.  
  30. std::pair<int,int> squared_solution (const hiddenVector &v) {
  31.   int n = v.getSize();
  32.   for (int a = 0; a < n; ++a) {
  33.     int b;
  34.     for (b = a+1; b < n; ++b)
  35.       if (!v.iLessThanj(a, b) && !v.iLessThanj(b, a))
  36.         return {a, b};
  37.     if (b < n)
  38.       return {};
  39.   }
  40.   return {};
  41. }
  42.  
  43. std::pair<int,int> sublinear_solution (const hiddenVector &v) {  
  44.   std::vector<int> a_ind (v.getSize ());
  45.   for (int i = 0; i < v.getSize(); i++)
  46.     a_ind[i] = i;
  47.  
  48.   std::sort(begin(a_ind), end(a_ind), [&v] (int i, int j) {
  49.     return v.iLessThanj(i, j); });
  50.  
  51.   for (int k = 0; k < a_ind.size()-1; k++)
  52.     if (!v.iLessThanj(a_ind[k], a_ind[k+1]))
  53.       return {a_ind[k], a_ind[k+1]};
  54.  
  55.   return {};
  56. }
  57.  
  58. void run_tests (int N, int test_num) {    
  59.   std::chrono::duration<double> squared_time (0);
  60.   std::chrono::duration<double> sublinear_time (0);  
  61.   for (int i = 0 ; i < test_num; i++) {
  62.     hiddenVector a (N);
  63.     // squared test
  64.     auto start = std::chrono::system_clock::now();
  65.     auto res_1 = squared_solution (a);
  66.     auto end = std::chrono::system_clock::now();
  67.     squared_time += end-start;
  68.     // sublinear test
  69.     start = std::chrono::system_clock::now();
  70.     auto res_2 = sublinear_solution (a);
  71.     end = std::chrono::system_clock::now();
  72.     sublinear_time += end-start;
  73.     // check correctness
  74.     if (res_1.first != res_2.first || res_1.second != res_2.second)
  75.       if (res_1.first != res_2.second || res_1.second != res_2.first)
  76.         throw 1;
  77.   }
  78.   // normalize time
  79.   squared_time /= test_num;
  80.   sublinear_time /= test_num;
  81.  
  82.   std::cout << squared_time.count() << " " <<
  83.                sublinear_time.count() << std::endl;
  84. }
  85.  
  86. int main () {
  87.   for (int N = 10; N < 1'000'000 ; N *= 10) {
  88.     std::cout << N << " ";
  89.     run_tests (N, 10 /*test_num*/);
  90.   }
  91.   return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement