-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
102 lines (99 loc) · 1.56 KB
/
split.cpp
File metadata and controls
102 lines (99 loc) · 1.56 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
void solve(vector<int> &arr)
{
int n = arr.size();
unordered_map<int,int> mpp;
for(auto &x:arr)
{
mpp[x]++;
}
int ans=0;
int l_sz=n/2,r_sz=n/2;
int flag = 1;// signifying 1 --> Odd odd, 0--> even even else -1 for odd even
for(auto it:mpp)
{
int freq = it.second;
int a,b;
if(freq%2==1)
{
if(max(l_sz,r_sz)>freq/2)
{
a = freq/2;
b=a+1;
flag = -1;
}
else
{
a = max(l_sz,r_sz);
b = l_sz+r_sz-max(l_sz,r_sz);
if(a%2==1 && b%2==1)
flag = 1;
else if(a%2==0 && b%2==0)
flag = 0;
else flag = -1;
}
}
else
{
if(max(l_sz,r_sz)>freq/2)
{
a = freq/2-1;
b = freq/2+1;
flag = 1;
}
else
{
a = max(l_sz,r_sz);
b = l_sz+r_sz-max(l_sz,r_sz);
if(a%2==1 && b%2==1)
flag = 1;
else if(a%2==0 && b%2==0)
flag = 0;
else flag = -1;
}
}
if(l_sz>r_sz)
{
l_sz = l_sz - max(a,b);
r_sz = r_sz - (a+b-max(a,b));
}
else
{
r_sz = r_sz - max(a,b);
l_sz = l_sz - (a+b-max(a,b));
}
if(flag==1)
{
ans+=2;
}
else if(flag==0)
ans+=0;
else ans+=1;
flag = 1;
}
cout<<ans<<endl;
return;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
cin>>t;
while(t--)
{
int n;
cin>>n;
n*=2;
vector<int> arr(n);
for (auto &a:arr)
cin>>a;
solve(arr);
}
return 0;
}