Two heaps -codeforce 353B --mystical_curve

Valera has 2·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 2·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 2·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.

Example
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.
题意:把所有的数字分成两堆,由这两堆中数字组成一个四位数,使得不同的四位数最多
解题策略:由题意可以知道两堆中不同的数分别是x,y,则最多有x*y个,所以先把所有的数排序,然后依次分到1,2堆中,如果原堆中已经有话就当到最后分组以确保两堆中个数相等。

#include <iostream>
#incl  ude <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int inf = 220;
int n;
struct node
{
    int x,pos,m;
    node(){};
    node(int x,int pos):x(),pos(){};
    bool operator<(const node &m)
    {
        return x<m.x;
    }
}s[inf],m[inf];
int main()
{
    cin>>n;n*=2;
    for(int i=0;i<n;i++){
        cin>>s[i].x;
        s[i].m=i;
    }
    copy(s,s+n,m);
    sort(s,s+n);
    int cunt1=0,cunt2=0,x,y,d;
    cunt1++;
    x=s[0].x;m[s[0].m].pos=1;
    int h[inf],l=0;
    for(int i=1;i<n;i++)
    {
        d=s[i].x;
        if(i==1){
           y=d;cunt2++;m[s[1].m].pos=2;
            continue;   
        }
        if(cunt2<cunt1){
            if(d==y){
                h[l++]=s[i].m;
                continue;
            }
            cunt2++;m[s[i].m].pos=2;y=d;
        }else
        {
            if(d==x){
                h[l++]=s[i].m;
                continue;
            }
            cunt1++;m[s[i].m].pos=1;x=d;
        }
    }
    cout<<cunt1*cunt2<<endl;
    for(int i=0;i<l;i++)
    {
        if(cunt1<cunt2){
            cunt1++;m[h[i]].pos=1;
        }else
        {
            cunt2++;m[h[i]].pos=2;
        }
    }

    for(int i=0;i<n;i++)
        cout<<m[i].pos<<(i==n-1?"\n":" ");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值