zoukankan      html  css  js  c++  java
  • 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][2];
     
    int num1[N],num2[N];
     
    void read(int &x)
    {
        x=0; char c=getchar();
        while(!isdigit(c)) c=getchar();
        while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
    }
     
    int main()
    {
        int T;
        read(T);
        int n;
        int tot1,tot2;
        while(T--)
        {
            memset(sum,0,sizeof(sum));
            memset(wh,0,sizeof(wh));
            read(n);
            for(int i=1;i<=n;++i)
            {
                read(a[i]);
                sum[a[i]]++;
                if(wh[a[i]][0]) wh[a[i]][1]=i;
                else wh[a[i]][0]=i;
            }
            tot1=tot2=0;
            for(int i=1;i<N;++i)
            {
                if(sum[i]&1) num1[++tot1]=i;
                else if(sum[i]) num2[++tot2]=i;
            }
            
            if(tot1==1 &&  tot2==1) 
            {
                cout<<n-1<<'
    ';
                b[wh[num1[1]][0]]=num1[1];
                b[wh[num2[1]][0]]=b[wh[num2[1]][1]]=num2[1]; 
                swap(b[wh[num1[1]][0]],b[wh[num2[1]][0]]);
            }
            else if((tot1==1 && !tot2) || (tot2==1 && !tot1)) 
            {
                cout<<0<<'
    ';
                for(int i=1;i<=n;++i) b[i]=a[i]; 
            } 
            else 
            {
                cout<<n<<'
    ';
                if(tot1==1)
                {
                    for(int i=1;i<tot2;++i) b[wh[num2[i]][0]]=num2[i+1],b[wh[num2[i]][1]]=num2[i+1];
                    b[wh[num2[tot2]][0]]=b[wh[num2[tot2]][1]]=num2[1];
                    b[wh[num1[1]][0]]=num1[1];
                    swap(b[wh[num2[1]][0]],b[wh[num1[1]][0]]);
                }
                else if(tot2==1)
                {
                    for(int i=1;i<tot1;++i) b[wh[num1[i]][0]]=num1[i+1];
                    b[wh[num1[tot1]][0]]=num1[1];
                    b[wh[num2[1]][0]]=b[wh[num2[1]][1]]=num2[1]; 
                    swap(b[wh[num2[1]][0]],b[wh[num1[1]][0]]);
                    swap(b[wh[num2[1]][1]],b[wh[num1[2]][0]]);
                }
                else
                {
                    for(int i=1;i<tot1;++i) b[wh[num1[i]][0]]=num1[i+1];
                    b[wh[num1[tot1]][0]]=num1[1];
                    for(int i=1;i<tot2;++i) b[wh[num2[i]][0]]=num2[i+1],b[wh[num2[i]][1]]=num2[i+1];
                    b[wh[num2[tot2]][0]]=b[wh[num2[tot2]][1]]=num2[1];
                }
            }
            for(int i=1;i<=n;++i) cout<<b[i]<<' ';
            cout<<'
    ';
        }
    } 

    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
  • 相关阅读:
    非常适合新手的一个Python爬虫项目: 打造一个英文词汇量测试脚本!
    代码遇到异常怎么办?不要慌,来看看这个!
    老司机要开车了!用Selenium+PhantomJS来抓取煎蛋网妹子图
    Python基础之格式化输出函数format()功能详解
    Python基础之常用格式化输出字符详解
    新手学python,如何才能更快升职加薪,迎娶白富美,走上人生巅峰
    ASP.NET MVC学习笔记 第二天
    ASP.NET MVC学习笔记 第一天
    ActiveMQ相关:
    WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8059993.html
Copyright © 2011-2022 走看看