1.快排中查找顺序不能变,因为当数据为5 4 3 2 1时,5和1进行交换,其基点改变了
2.再写并查集时,将根定义为前面那个数,不然碰到1 2,2 3,3 4,4 5这种情况还要进行一步操作,增加时间复杂度
3.假期安排:look look 其他算法,学习一下c++
AB 每队 2n2n 人正在玩石头剪刀布。A 队第 ii 个人出 a_iai,B 队第 ii 个人出 b_ibi。编号相同的人会对战。若 A 队赢则加一分,平不得分,输扣一分。你可以至多改变每队 nn 个人的出拳方案,使得 A 队的得分最高。输出得分的最大值和任意一组构造方案。
本题中,我们用 11 代表石头,22 代表剪刀,33 代表布。
输入输出样例
输入 #1复制
2 1 1 2 1 2 2 2 3 1 3 1 2 2 1
输出 #1复制
2 1 1 2 2 4 3 1 1 3 1 2 2 1
说明/提示
Explanation
For the first test case, we can change a_2a2 to 11 and b_1b1 to 22. Then Team A can get 22 points. It can be proved that this is the maximum score that Team A can get.
For the second test case, we can change a_1a1 to 33 and a_2a2 to 11.
Constraints
1\le T,n \le 10^5;\ 1\le a_i,b_i \le 31≤T,n≤105; 1≤ai,bi≤3. The sum of nn over all test cases \le 10^5≤105.
#include<iostream>
using namespace std;
int a[1000005],b[1000005];
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
{
int m,s;
cin>>m;
for(int j=0; j<2*m; j++)
cin>>a[j];
for(int j=0; j<2*m; j++)
cin>>b[j];
int h=m;
for(int j=0; j<2*m; j++)
{
if(a[j]-b[j]==1||a[j]-b[j]==-2||a[j]==b[j])//A不能得分的情况
{
if(h>0)//前m个改变a使得a得分
{
if(b[j]==1)
a[j]=3;
else if(b[j]==2)
a[j]=1;
else if(b[j]==3)
a[j]=2;
h--;
}
else//后m个改变b使得a得分
{
if(a[j]==1)
b[j]=2;
else if(a[j]==2)
b[j]=3;
else if(a[j]==3)
b[j]=1;
}
}
}
s=2*m;//a总是能得分
cout<<s<<endl;
for(int j=0; j<2*m; j++)
cout<<a[j]<<' ';
cout<<endl;
for(int j=0; j<2*m; j++)
cout<<b[j]<<' ';
cout<<endl;
}
}
开始时把题目看成了总共改变m个,结果考虑了很多种情况。后来发现是a,b各最多改变m个,这说明可以全部改成a赢,所以只要前m次a改变使得a赢,后m次b改变使得a赢