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. python learning GUI

    Hello world1 from tkinter import * # 第一步是导入Tkinter包的所有内容 class Application(Frame): # 第二步是从Frame派生一个A ...

  2. JAVA自学日记——Part Ⅱ

    今天学习了类与对象,其中关于this关键字的用法,static静态变量与静态方法,以及引用传递需要特别注意一下. 首先是引用传递: 在本段程序中可以通过进行外部对类对象的属性赋值来更改,同时也可以通过 ...

  3. 详解Android微信登录与分享

    Android 使用微信登录.分享功能 具体的文档详情微信官网上介绍(微信官网文档),本人直接按照项目部署步骤进行讲解: 第一步:申请你的AppID: 第二步:依赖 dependencies { co ...

  4. HDU 2107 Founding of HDU

    http://acm.hdu.edu.cn/showproblem.php?pid=2107 Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等A ...

  5. vue开发完成后打包后图片路径不对

    用vue做了一个小的移动端项目,从头到尾做下来,感觉自己好多东西都没弄清楚过.也学到了很多,已整理笔记在自己电脑上,但是比较零散,空了再来仔细整理整理. 于是,上周五模拟好数据(接口还未写),准备打包 ...

  6. syntax error:unexpected end of file

    将window上编辑的xxy1.sh脚本上传到linux上,并执行的时候提示 xxy1.sh: line 17: syntax error: unexpected end of file 但是通过ca ...

  7. htop操作方法

    为什么 Linux 的 htop 命令完胜 top 命令? 在 Linux 系统中,top 命令用来显示系统中正在运行的进程的实时状态,它显示了一些非常有用的信息,比如 CPU 利用情况.内存消耗情况 ...

  8. Python Matplotlib绘图库 安装

    一般我们在做科学计算的时候,首先会想到的是matlab,但是呢,一想到matlab安装包那么大,我就有点不想说什么了. Matplotlib 是python最著名的绘图库,它提供了一整套和matlab ...

  9. c++ 替换修改一个文件夹下的所有文件的文件名

    代码简洁,亲测可用. 1,首先来获取(输出)一个文件夹中所有的文件名 void getFiles(string path, vector<string>& files) { //文 ...

  10. Logrotate还有谁记得它??

    我发现很多人的服务器上都运行着一些诸如每天切分Nginx日志之类的CRON脚本,大家似乎遗忘了Logrotate,争相发明自己的轮子,这真是让人沮丧啊!就好比明明身边躺着现成的性感美女,大家却忙着自娱 ...