konchin_shih

merge sort

Feb 27th, 2021
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include".\template\headers.hpp"
  2.  
  3. // #define MULTI_TASKCASE
  4. using namespace abb;
  5. using namespace output;
  6. using namespace rit;
  7. using namespace wit;
  8.  
  9. inline void init() {
  10.  
  11. }
  12.  
  13. constexpr int n = 1e5;
  14.  
  15. void merge_sort(A<int, n>& arr, int l = 0, int r = n) {
  16.     if (r - l < 2) return;
  17.     static A<int, n> tmp;
  18.     int m = (l + r) >> 1;
  19.     merge_sort(arr, l, m);
  20.     merge_sort(arr, m, r);
  21.     auto t = tmp.begin(), a = arr.begin();
  22.     copy(a + l, a + m, t + l);
  23.     copy(a + m, a + r, t + m);
  24.     int i = l, lp = l, rp = m;
  25.     while (lp < m && rp < r) {
  26.         if (t[lp] < t[rp])
  27.             a[i++] = t[lp++];
  28.         else
  29.             a[i++] = t[rp++];
  30.     }
  31.     while (lp < m)
  32.         a[i++] = t[lp++];
  33.     while (rp < r)
  34.         a[i++] = t[rp++];
  35. }
  36.  
  37. inline void solve() {
  38.     static A<int, n> v;
  39.     A<double, 20> arr;
  40.     fstream filein("in.txt", ios::in);
  41.     cin.rdbuf(filein.rdbuf());
  42.     for (auto& k : arr) {
  43.         cout << "poop" << ' ';
  44.         for (auto& i : v)
  45.             cin >> i;
  46.         clock_t before = clock();
  47.         merge_sort(v);
  48.         clock_t after = clock();
  49.         k = double(after - before) / CLOCKS_PER_SEC * 1000;
  50.     }
  51.     filein.close();
  52.     cout << endl << flush;
  53.     fstream fileout("out.txt", ios::out);
  54.     cout.rdbuf(fileout.rdbuf());
  55.     for (int i = 0; i != 20; i++)
  56.         cout << arr[i] << endl;
  57.     fileout.close();
  58. }
  59.  
  60. main() {
  61.     ios::sync_with_stdio(false);
  62.     init();
  63.     int t = 1;
  64. #ifdef MULTI_TASKCASE
  65.     cin >> t; cin.ignore();
  66. #endif
  67.     while (t--) solve();
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment