很有意思的一道贪心题。
颜色和方向一共有四种组合,它们的代价分别是:
color | direction | cost |
---|---|---|
$\ne$ | $\ne$ | 1 |
$\ne$ | $=$ | 2 |
$=$ | $\ne$ | 0 |
$=$ | $=$ | 1 |
对于代价是 $0$ 的情况,先使用归并步骤把它们都去掉。剩下的 $3$ 种情况我们应该尽量避免第 $2$ 种代价为 $2$ 的情况,或者让它的个数最少。也就是方向相同的袜子中,尽量避免出现颜色不同的情况。
假设剩余的两个方向的袜子个数分别是 $len_1, len_2$,不妨设 $len_1 \le len_2$,我们需要让 $len_2$ 中颜色个数为奇数的袜子各拿出一个来,去和 $len_1$ 匹配,这样就尽量减少了第 $2$ 种情况出现,同时原来个数为奇数的袜子的个数都变成了偶数,个数为偶数的袜子比较好处理。这里贪心的根据是:剩余袜子中,任何两个被消除的最少代价是 $1$,因此尽量减少代价 $2$ 的即可。这样做完之后,再根据奇偶性分类讨论 $len_2$ 中剩余的袜子,容易发现可以根据奇偶性减少很多讨论,最终只有两种情况。
// Date: Thu Jan 11 19:36:48 2024
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353;
const ll INFL = 0x3f3f3f3f'3f3f3f3f;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nemp(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define NL cout << '\n';
template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <typename t> istream &operator>>(istream &in, vector<t> &vec) {
for (t &x : vec)
in >> x;
return in;
}
template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) {
int n = SZ(vec);
For(i, 0, n) {
out << vec[i];
if (i < n - 1)
out << ' ';
}
return out;
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#define dbg(x...) \
cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << "\e[39m" << endl;
#else
#define debug1
#define debug2
#define debug3
#define dbg(x...)
#endif
const int N = 200100;
int n, l, r, a[N], b[N], a1[N], b1[N];
void solve() {
cin >> n >> l >> r;
For1(i, 1, l) cin >> a[i];
For1(i, 1, r) cin >> b[i];
sort(a + 1, a + 1 + l);
sort(b + 1, b + 1 + r);
int res = 0, l1 = 0, r1 = 0, i = 1, j = 1;
while (i <= l && j <= r) {
if (a[i] == b[j]) {
++i;
++j;
} else if (a[i] > b[j]) {
b1[++r1] = b[j++];
} else {
a1[++l1] = a[i++];
}
}
while (i <= l)
a1[++l1] = a[i++];
while (j <= r) {
b1[++r1] = b[j++];
}
int len1 = l1, len2 = r1, cnt2 = 0;
map<int, int> m;
if (len1 >= len2) {
For1(i, 1, l1) m[a1[i]]++;
swap(len1, len2);
} else {
For1(i, 1, r1) { m[b1[i]]++; }
}
for (auto &[x, cnt] : m) {
if (cnt & 1) {
cnt2++;
}
}
if (cnt2 >= len1) {
res = cnt2 + (len2 - cnt2) / 2;
} else {
res = len1 + (len2 - len1) / 2;
}
cout << res << '\n';
}
int main(void) {
#ifdef _DEBUG
freopen("1515d.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}