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. Maven教程--02设置Maven本地仓库|查看Maven中央仓库

    一:设置Maven本地仓库 Maven默认仓库的路径:~\.m2\repository,~表示我的个人文档:例如:C:\Users\Edward\.m2\repository:如下图: Maven的配 ...

  2. srTCM和trTCM介绍

    本文是用于QoS Meter功能的算法的RFC的阅读笔记.DPDK的QoS_meter示例程序用的就是这个算法. srTCM srTCM的英文全称是Single Rate Three Color Ma ...

  3. Beta冲刺 (7/7)

    队名:天机组 组员1友林 228(组长) 今日完成:封装代码 明天计划: 剩余任务:优化网络通讯机制 主要困难:暂无 收获及疑问:暂无 组员2方宜 225 今日完成:优化了一部分活动 明天计划:剩余活 ...

  4. Unity发布Windows程序遇到的问题

    Unity版本:5.6.2 因为程序中使用了Networking模块,所以在打包发布的时候需要登录Unity的账号,并做设置. 错误信息如下: 解决办法如下: 先登录Unity账号,并在Service ...

  5. 程序开发入门工具之CodeBlocks

    程序开发基础工具之CodeBlocks 作为程序开发工作者,我们会接触很多的程序开发软件:但实用以及容易掌握的程序开发软件对于初学者的学习能力是有一定的加成的.今天我就作为一个程序开发者给大家推荐一个 ...

  6. thinkphp学习3-模板与视图

    1.模板赋值 如果要在模板中输出变量,必须在在控制器中把变量传递给模板,系统提供了assign方法对模板变量赋值,无论何种变量类型都统一使用assign赋值. $this->assign('na ...

  7. #Leetcode# 707. Design Linked List

    https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...

  8. 如何在Sql Server 2000中用一条语句实现树的查询

    以公司的网站栏目表为例,表结构(表名:Subject)如下:   字段名称 字段类型 字段长度 id int 4 NAME varchar 200 PARENTID int 4 CODE varcha ...

  9. javascript 容易忘记方法集锦

    定义和用法 1.split() 方法用于把一个字符串分割成字符串数组. 提示: 如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间 ...

  10. C++中的栈内存和堆内存的区别

    数据结构中的堆与栈: 栈:是一种连续储存的数据结构,具有先进后出的性质.通常的操作有入栈(圧栈).出栈和栈顶元素.想要读取栈中的某个元素,就要将其之前的所有元素出栈才能完成.类比现实中的箱子一样. 堆 ...