题意
有n
(偶数)只袜子,前l
只是左脚的,后r
只为右脚的,每个袜子都有一个颜色,需要把所有的袜子成对(每个袜子必须有一个左脚和一个右脚相对应,且颜色相同)
可以通过一些操作来实现,每个操作需要1元
1.给一个袜子换一个颜色
2.给一个左脚的袜子变为右脚的
3.给一个右脚的袜子变为左脚的
求最小代价
思路
两个袜子成对有4种情况
1.一个左脚和一个右脚相同的 代价为0
2.一个左脚和右脚不同的 代价为1
3.两个左脚相同的 代价为1
4.两个右脚相同的 代价为1
5.两个左脚不相同的 代价为2
6.两个右脚不同的 代价为2
所以在满足代价最小的情况下,应该尽可能的满足。
操作:
1.如果满足1,就减去对应的袜子
2.如果满足3,4 就减去对应的袜子
3.剩下的袜子尽可能的满足 2(感觉操作2,3可以换下顺序,不过这样过了
4.如果还有剩余的话可以从3,4中提取一些袜子
5.最后满足5,6
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<map>
using namespace std;
#define x first
#define y second
//#define int long long
typedef long long ll;
typedef pair<int ,int > PII;
const int N = 2e5 + 10;
int cnt[N];
int main(){
int T;
cin >> T;
while(T--){
int n,l,r;
scanf("%d%d%d",&n,&l,&r);
for(int i = 1;i <= n;i++){
int x;
scanf("%d",&x);
if(i <= l) cnt[x] --;
else cnt[x] ++;
}
int lcnt = 0,rcnt = 0;
int lx = 0,rx = 0;
int ans = 0;
for(int i = 1;i <= n;i++){
if(cnt[i] < 0){
lcnt -= cnt[i];
if((-cnt[i]) & 1) lx ++;
ans += (-cnt[i]) / 2;
}
else{
rcnt += cnt[i];
if(cnt[i] & 1) rx ++;
ans += cnt[i] / 2;
}
cnt[i] = 0;
}
if(lx < rx) swap(lcnt,rcnt),swap(lx,rx);
// cout << ans << endl;
ans += lx;
int t = min(rcnt,lx);
ans -= (t - rx) / 2;
lx -= t;
// ans += lx;
cout << ans <<endl;
}
return 0 ;
}