https://www.codechef.com/DEC17/problems/CHEFHAM

#include<cstdio>
#include<cstring>
#include<iostream> using namespace std; #define N 100001 int a[N],b[N];
int sum[N],wh[N][]; int num1[N],num2[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} int main()
{
int T;
read(T);
int n;
int tot1,tot2;
while(T--)
{
memset(sum,,sizeof(sum));
memset(wh,,sizeof(wh));
read(n);
for(int i=;i<=n;++i)
{
read(a[i]);
sum[a[i]]++;
if(wh[a[i]][]) wh[a[i]][]=i;
else wh[a[i]][]=i;
}
tot1=tot2=;
for(int i=;i<N;++i)
{
if(sum[i]&) num1[++tot1]=i;
else if(sum[i]) num2[++tot2]=i;
} if(tot1== && tot2==)
{
cout<<n-<<'\n';
b[wh[num1[]][]]=num1[];
b[wh[num2[]][]]=b[wh[num2[]][]]=num2[];
swap(b[wh[num1[]][]],b[wh[num2[]][]]);
}
else if((tot1== && !tot2) || (tot2== && !tot1))
{
cout<<<<'\n';
for(int i=;i<=n;++i) b[i]=a[i];
}
else
{
cout<<n<<'\n';
if(tot1==)
{
for(int i=;i<tot2;++i) b[wh[num2[i]][]]=num2[i+],b[wh[num2[i]][]]=num2[i+];
b[wh[num2[tot2]][]]=b[wh[num2[tot2]][]]=num2[];
b[wh[num1[]][]]=num1[];
swap(b[wh[num2[]][]],b[wh[num1[]][]]);
}
else if(tot2==)
{
for(int i=;i<tot1;++i) b[wh[num1[i]][]]=num1[i+];
b[wh[num1[tot1]][]]=num1[];
b[wh[num2[]][]]=b[wh[num2[]][]]=num2[];
swap(b[wh[num2[]][]],b[wh[num1[]][]]);
swap(b[wh[num2[]][]],b[wh[num1[]][]]);
}
else
{
for(int i=;i<tot1;++i) b[wh[num1[i]][]]=num1[i+];
b[wh[num1[tot1]][]]=num1[];
for(int i=;i<tot2;++i) b[wh[num2[i]][]]=num2[i+],b[wh[num2[i]][]]=num2[i+];
b[wh[num2[tot2]][]]=b[wh[num2[tot2]][]]=num2[];
}
}
for(int i=;i<=n;++i) cout<<b[i]<<' ';
cout<<'\n';
}
}

Read problems statements in Vietnamese

.

Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A.

The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance betweenA and B is the number of indices i (1 ≤ i ≤ N) such that Ai ≠ Bi.

The brother needs your help in finding any such array B. Can you please find one such array for him?

Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains an integer N denoting the length of the array A.
  • The second line contains N space-separated integers A1, A2 ... AN.

Output

  • For each test case, print two lines.
  • The first line should contain the maximum possible Hamming distance that array Bcan have from array A.
  • The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of Bi. Note that B should be an array obtained after shuffling the elements of A.

Constraints

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ Ai ≤ 105
  • The frequency of each integer in the array A will be at most 2.

Subtasks

Subtask #1 (30 points): all elements in the array A are unique

Subtask #2 (30 points): 5 ≤ N ≤ 105

Subtask #3 (40 points): original constraints

Example

Input

3
2
1 2
3
1 2 1
4
2 6 5 2 Output 2
2 1
2
2 1 1
4
6 2 2 5

CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays的更多相关文章

  1. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  2. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

    https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...

  3. CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

    https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...

  4. CF&&CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out

    https://www.codechef.com/DEC17/problems/CPLAY #include<cstdio> #include<algorithm> using ...

  5. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  6. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  7. CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)

    http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...

  8. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  9. CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle

    http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...

随机推荐

  1. BNUOJ 52305 Around the World 树形dp

    题目链接: https://www.bnuoj.com/v3/problem_show.php?pid=52305 Around the World Time Limit: 20000msMemory ...

  2. 【TCP/IP详解 卷一:协议】第六章:DHCP 和自动配置

    简介 为了使用 TCP/IP 协议族,每台主机or路由器都需要一定的配置信息: IP地址 子网掩码 广播地址 路由或转发表 DNS 协议配置方法: 手动 通过使用网络服务来获得 使用一些算法来自动确定 ...

  3. angularJS1笔记-(11)-自定义指令(transclude/priority/terminal)

    自定义指令的属性 transclude:为true时,允许把html中新定义的指令中原来的dom运用到该指令的template中. 属性priority,设置该指令的优先级,优先级大的先执行,默认指令 ...

  4. python下的Box2d物理引擎的配置

    /******************************* I come back! 由于已经大四了,正在找工作 导致了至今以来第二长的时间内没有更新博客.向大家表示道歉 *********** ...

  5. OSG学习:裁剪变换(2)

    接着上一篇博客说. 还有一种裁剪的方法:osg::Scissor类. 这个类封装了OpenGL中的glScissor()函数. 该类主要用于设置一个视口裁剪平面举行.设置裁剪平面举行的函数如下: vo ...

  6. 复利计算测试(C语言)

    对我们和复利计算程序,写单元测试. 有哪些场景? 期待的返回值 写测试程序. 运行测试. 测试模块 测试输入 预期结果 运行结果 bug跟踪 计算终值 (本金,年限,利率) 终值     1 (100 ...

  7. 第十周(11.18-11.24)----个人项目----学习java总结2

    一.获取随机数 方法1  (数据类型)(最小值+Math.random()*(最大值-最小值+1)) ,注意这里的每一个括号最好都不要省略掉. 例: public static void main(S ...

  8. 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" task.

    在安装一个从XE6复制到XE4的控件时出现编译错误: [MSBuild Error] "0" is an invalid value for the "DebugInfo ...

  9. notepad++ 安装go插件

    1. 想学习go语言 使用notepad++ 但是发现无法安装 gonpp的插件 花了很长时间. 发现问题为: 前几天将notepad++ 升级到了 7.6 的版本 然后使用 plugin manag ...

  10. 怎样使用ADO中的UpdateBatch方法(200分)

    诸位: 我在使用ADO组件(ADOQuery.ADODataSet)的BatchUpdate模式时,系统竟不认识UpdateBatch.CancelBatch方法.这是怎么回事?我的运行环境是Win2 ...