cf #205 B Codeforces Round #205 (Div. 2) B. Two Heaps

B. Two Heaps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera has n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.

Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.

Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?

Input

The first line contains integer n (1 ≤ n ≤ 100). The second line contains n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.

Output

In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.

If there are multiple optimal ways to split the cubes into the heaps, print any of them.

Sample test(s)
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note

In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.

In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.

 

按元素的出现次数处理按一下顺序处理:

1.若某元素出现了至少2次,若偶数次,均分至heap 1、heap 2;否则,最后留1个该元素,第3步处理;   ==记录heap中不同元素数量

2.若某元素出现了1次,优先分配到当前不同元素数量少的heap;                   ==更新heap中不同元素数量

3.对于1.中剩余的元素,一个个往元素数量少的heap丢即可。

最终可得最多的不同整数为 number of  different numbers in heap 1  multiply ... in heap 2.

为什么这么做答案正确?  若a出现n次,暴力枚举分配方式:(1, n-1),(2, n-2)。。。最终发现(n=1, ..., 3)类似于均值不等式的式子。。。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 222
int togo[MAXN], a[MAXN];
int num[MAXN], ln[MAXN], rn[MAXN];

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int n;
    while(cin>>n){
        n = n << 1;
        fill_n(ln, n, 0);       fill_n(rn, n, 0);
        fill_n(togo, n, 0);     fill_n(num, n, 0);
        int i, j, lft = 0, rit = 0;
        for(i=0; i<n; i++)
            cin>>a[i], num[a[i]]++;
        for(i=10; i<101; i++) if(num[i]>1){
            int tn = num[i], j, heap=1;
            for(j=0; j<n; j++) if(a[j]==i){
                if(heap==1 && (ln[i]++ == 0)) lft++;
                else if(heap==2 && (rn[i]++ == 0)) rit++;
                togo[j]=heap;   heap=3-heap;    tn--;
                if(heap==1 && tn==1) break;             //odd number of same cubes
            }
        }

        for(i=0; i<n; i++) if(num[a[i]]==1){
            if(lft>rit) togo[i]=2, rit++;
            else togo[i]=1, lft++;
        }
        int ans = lft * rit ;
        for(i=0; i<n; i++) if(!togo[i]){
            if(lft>rit) togo[i]=2, rit++;
            else togo[i]=1, lft++;
        }
        cout<<ans<<endl<<togo[0];
        for(i=1; i<n; i++)
            cout<<' '<<togo[i];
        cout<<endl;
    }
    return 0;
}

 

posted @ 2013-10-11 22:33  Ramanujan  阅读(321)  评论(0编辑  收藏  举报