forked from srbcheema1/Algo_Ds
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnewthird.cpp
More file actions
78 lines (77 loc) · 1.82 KB
/
newthird.cpp
File metadata and controls
78 lines (77 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
#define db1(x) cerr << #x << "=" << x << '\n'
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << '\n'
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << '\n'
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define mp make_pair
#define sp << ' ' <<
#define pb push_back
#define FastIO \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
using ll = long long;
void merge(vector<int> &A, int beg, int mid, int end, int &count)
{
int size = end - beg;
vector<int> New;
int i = beg, j = mid;
while (i < mid && j < end)
{
if (A[i] <= A[j])
{
New.push_back(A[i++]);
}
else
{
count++;
New.push_back(A[j++]);
}
db1(count);
}
while (i < mid)
{
New.push_back(A[i++]);
}
while (j < end)
{
New.push_back(A[j++]);
}
rep(i, New.size()) cerr << New[i] << " ";
cout << endl;
// copy to the array the result
for (int i = beg, j = 0; i < end; i++, j++)
{
A[i] = New[j];
}
}
void merge_sort(vector<int> &A, int beg, int end, int &count)
{
int mid = (beg + end) / 2;
merge_sort(A, beg, mid, count);
merge_sort(A, mid, end, count);
merge(A, beg, mid, end, count);
}
int main()
{
FastIO
ll t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
vector<int> v(n);
rep(i, n) cin >> v[i];
int count = 0;
merge_sort(v, 0, n, count);
rep(i, n) cout << v[i] << " ";
cout << endl;
cout << count << endl;
cout << '\n';
}
return 0;
}