首页 > 其他分享 >Codeforces Global Round 14, D. Phoenix and Socks

Codeforces Global Round 14, D. Phoenix and Socks

时间:2023-02-08 16:06:50浏览次数:46  
标签:ld 14 Phoenix int Global sock socks rc lc


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;
}


标签:ld,14,Phoenix,int,Global,sock,socks,rc,lc
From: https://blog.51cto.com/gwj1314/6044563

相关文章

  • 【CCCC】L3-014 周游世界 (30分),,DFS搜索最短路,路径打印
    problemL3-014周游世界(30分)周游世界是件浪漫事,但规划旅行路线就不一定了……全世界有成千上万条航线、铁路线、大巴线,令人眼花缭乱。所以旅行社会选择部分运输公司组......
  • pycharm爬虫报错:pymysql.err.DataError: (1406, "Data too long for column 'content'
     在学习爬虫的时候,获取数据存入mysql时出现了问题:pymysql.err.DataError:(1406,"Datatoolongforcolumn'content'atrow1")因为mysql也是本地搭建的,所以一时间......
  • U214268 不老不死的竹林引路人
    不老不死的竹林引路人特别感谢icyM3tra神提供的标程OrzOrzOrz。速度上总时间直接从25s(我原来的程序)飙到了5s,单个测试点最慢也只有800ms!虽然有这么快的代码,但我还......
  • 第14课、POM - Page Object
      fromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.support.uiimportSelectfromselenium.w......
  • CF14D题解
    CF14DTwoPaths题解题目链接传送门题意简述给定一棵树,找出两条不经过相同点的最长路径,使得他们的长度乘积最大。题目分析首先,如果在一棵树上,两条路径没有共同的点,那......
  • C++ Day14 借助智能指针实现文本查询查询
    一、设计思路数据结构:1、读取文件时,要记住文件的每一行,并且要将每一行分解为独立的单词vector<string>vec;istringstream2、输出时提供每个单词与其关联的行号,且......
  • Educational Codeforces Round 142 E. Divisors and Table
    链接:https://codeforces.com/problemset/problem/1792/E题意:给定n*n乘法表,第a[i][j]=i*j,给定m=m1*m2,求m的所有因子中出现在表中的最小行的xor和。n<=1e9,m......
  • 训练日记 2018.12.14
        哎,这几天被树形背包搞懵了,一开始感觉没学到啥,做一个题看一个题解,每个题单个来看都能看懂,但是遇到一个新题就不会了,而且你用上一个题的做法做,依旧不对,网上的题解......
  • Codeforces Round #514 (Div. 2).A. Cashier 做了个优化 还行
    A.Cashiertimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputVasyahasrecentlygotajobasacashieratal......
  • 洛谷 P2014 选课 树形依赖背包
    题目描述在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习。现在有N门功......