Codeforces Global Round 14, D. Phoenix and Socks

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Global Round 14, D. Phoenix and Socks相关的知识,希望对你有一定的参考价值。

problem

D. Phoenix and Socks
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
To satisfy his love of matching socks, Phoenix has brought his n socks (n is even) to the sock store. Each of his socks has a color ci and is either a left sock or right sock.

Phoenix can pay one dollar to the sock store to either:

recolor a sock to any color c′ (1≤c′≤n)
turn a left sock into a right sock
turn a right sock into a left sock
The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn’t change when it turns into a right sock, and vice versa.

A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make n/2 matching pairs? Each sock must be included in exactly one matching pair.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains three integers n, l, and r (2≤n≤2⋅105; n is even; 0≤l,r≤n; l+r=n) — the total number of socks, and the number of left and right socks, respectively.

The next line contains n integers ci (1≤ci≤n) — the colors of the socks. The first l socks are left socks, while the next r socks are right socks.

It is guaranteed that the sum of n across all the test cases will not exceed 2⋅105.

Output
For each test case, print one integer — the minimum cost for Phoenix to make n/2 matching pairs. Each sock must be included in exactly one matching pair.

Example
inputCopy
4
6 3 3
1 2 3 2 2 2
6 2 4
1 1 2 2 2 2
6 5 1
6 5 4 3 2 1
4 0 4
4 4 4 3
outputCopy
2
3
5
3
Note
In the first test case, Phoenix can pay 2 dollars to:

recolor sock 1 to color 2
recolor sock 3 to color 2
There are now 3 matching pairs. For example, pairs (1,4), (2,5), and (3,6) are matching.
In the second test case, Phoenix can pay 3 dollars to:

turn sock 6 from a right sock to a left sock
recolor sock 3 to color 1
recolor sock 4 to color 1
There are now 3 matching pairs. For example, pairs (1,3), (2,4), and (5,6) are matching.

solution

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int lc[maxn], rc[maxn];
int main(){
	int T;  cin>>T;
	while(T--){
		memset(lc,0,sizeof(lc));
		memset(rc,0,sizeof(rc));
		int n, l, r;  cin>>n>>l>>r;
		for(int i = 1; i <= n; i++){
			int c;  cin>>c;
			if(i<=l)lc[c]++;
			else rc[c]++;
		}
		int ld = l-n/2, rd = r-n/2;
		int lr = 0, rr = 0;
		int ans = 0;
		for(int i = 1; i <= n; i++){
			if(lc[i]==rc[i])continue;
			if(lc[i]>rc[i]){
				lc[i] -= rc[i];//0配对
				while(lc[i]>=2 && ld>0){//1变形状
					lc[i] -= 2;
					ld--;
					ans++;
				}
				//if(ld<=0){//1变颜色
					lr += lc[i];
				//}
			}else{
				rc[i] -= lc[i];
				while(rc[i]>=2 && rd>0){
					rc[i] -= 2;
					rd--;
					ans++;
				}
				//if(rd<=0){
					rr += rc[i];
				//}
			}
		}
		//cout<<ans<<":"<<lr<<","<<rr<<"\\n";
		//cout<<ld<<" "<<rd<<"\\n";
		if(ld>0 || rd>0){
			if(ld>0){
				ans += 2*ld;
				//rr -= ld;
				//ld = 0;
				lr -= 2*ld;
				//ans += lr;
			}
			if(rd>0){
				ans += 2*rd;
				//lr -= rd;
				//rd = 0;
				rr -= 2*rd;
				//ans += rr;
			}
		}
		if(lr!=0 && rr!=0){
			ans += lr;
		}
		cout<<ans<<"\\n";
	}
	return 0;
}

以上是关于Codeforces Global Round 14, D. Phoenix and Socks的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Global Round 1

Codeforces Global Round 1

Codeforces Global Round 1

Codeforces Global Round 1

Codeforces Global Round 19

Codeforces Global Round 1 AParity