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. Ubuntu登录界面添加root用户登录选项

    1.普通用户登录系统并打开终端 配置root密码 $sudo passwd 切换至root用户 $su root 输入密码 修改以下配置文件 $nano /usr/share/lightdm/ligh ...

  2. SQL Server 无法连接到本地服务器

    未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接:     解决办法: 在服务中启动SQL Server (MSSQLSERVER)这个服务.

  3. 由RS-232串口到PROFIBUS-DP总线的转换接口设计

    转自:http://gongkong.ofweek.com/2013-08/ART-310007-11001-28716256_2.html 1.PROFIBUS-DP网络协议 PROFIBUS的网络 ...

  4. 用windbg检查.NET线程池设置

    比如我们在machine.config中进行了这样的设置(8核CPU): <processModel maxWorkerThreads="100" maxIoThreads= ...

  5. 【vue】router-link 与 router-view

    1 router-link <router-link :to="{ path: '/hello', component: HelloWorld }">hello< ...

  6. Anaconda多版本Python管理以及TensorFlow版本的选择安装

    Anaconda是一个集成python及包管理的软件,记得最早使用时在2014年,那时候网上还没有什么资料,需要同时使用py2和py3的时候,当时的做法是同时安装Anaconda2和Anaconda3 ...

  7. 先验算法(Apriori algorithm) - 机器学习算法

    Apriori is an algorithm for frequent item set mining and association rule learning over transactiona ...

  8. tomcat设置虚拟路径映射服务器指定的物理路径

    在tomcat的server.xml中的host标签中加入如下标签: <Context crossContext="false" debug="1" do ...

  9. UIScrollView浏览一组图片,且图片与图片之间有间隔

    ---恢复内容开始--- UIScrollView是可以浏览一组view的,只要将其属性 pagingEnabled设置为true就可以了.具体过程是这样的, 1:将一组图片按照从左到右的顺序添加到U ...

  10. 解题:USACO14MAR Sabotage

    题面 题外话:我的实数二分有什么问题=.= 仍然(我为什么要这么说)是二分答案,如何检查呢?将所有的数减去二分出来的$mid$后求和得到和$sum$,然后如果在减出来的数列中能找出一段大于$sum$的 ...