CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
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的更多相关文章
- 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] 表示第 ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake
https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out
https://www.codechef.com/DEC17/problems/CPLAY #include<cstdio> #include<algorithm> using ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- 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的数 维护 ...
- CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)
http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- 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> # ...
随机推荐
- IEEE 802.11 无限局域网
(1)无线通讯的两个重要特征 ——Hidden node problem 双方虽然听不到对方的讯号,但同时传送给相同的对象导致了碰撞(这个时候双方都不知道发生了碰撞) ——Exposed node p ...
- Android自定义View实现仿QQ实现运动步数效果
效果图: 1.attrs.xml中 <declare-styleable name="QQStepView"> <attr name="outerCol ...
- 个人阅读&个人总结
个人阅读作业+总结 助教推荐的那些文章都是软件工程上的经典文章,阅读后感受到软件工程本身的深度,之前学习的软件工程都只是皮毛之中的皮毛而已.随着软件规模的越来越庞大,软件工程已经成为了软件开发中的必备 ...
- ubuntu下安装vsftpd及vsftpd配置文件不见的解决办法
利用命令 sudo apt-get install vsftpd //安装 进入etc文件可以找到 vsftpd.conf的配置文件 作为新手难免会弄错配置又不知道怎么办,那么可能会利用 sudo ...
- [学习]Windows server 使用控制台时容易卡死的解决方法
公司使用Windows server 下面的 cmd 命令行 控制台打开某一个 bat 文件的方式 进行后台使用.. 但是经常发现在winserver 2016 时 遇到卡死的情况, 今天中午我再进行 ...
- Node初识笔记 1第一周
#下载安装好node > https://nodejs.org/en/ # 打开cmd 调整好执行路径 . 1.js是JS文件名,cd调招路径,‘node’+空格 +JS文件名(带上扩展名) ...
- Delphi 判断一个字符串是否为数字
//函 数 名: IsDigit//返 回 值: boolean//日 期:2011-03-01//参 数: String//功 能: 判断一个字符串是否为数字// ...
- 我项目中使用userData的实例 UserData.js
关于userData的介绍,请参见http://hi.baidu.com/kaisep/blog/item/1da9a3312d2da5a15edf0e87.htmlhttp://hi.baidu.c ...
- Nagios学习笔记
1 Nagios功能 1.1 监控工具 1.2 可以监控主机/服务或者资源 1.3 四种状态值 OK,WARNING,CRITICAL,UNKNOWN CPU:90%(CRITICAL),80% ...
- 【Java并发编程】之十三:生产者—消费者模型
生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据. 这里实现如下情况的生产--消费模型: 生产者不断交替地生产两组 ...