CodeForces 353B Two Heaps

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 number1099. 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.


题意:2 * n个两位数,平均分成两份,在两组中分别取一个两位数,第一组中的作为千位和百位,第二组中的作为十位和个位。求可以组成的四位数的个数最大值。

要组成尽可能多的四位数,就要使每一组中重复的两位数尽可能少,最后个数等于两组中不同的两位数的个数之积。

先把所有数从小到大排序,把重复次数超过两次的数拎出来单独分组,把它们平均分到两个组中。再将剩余的数的组号依次循环赋值为1、2。时间复杂度O(N)。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int ans[205];

struct num
{
    int id;
    int v;
} a[205];

bool cmp(num x, num y)
{
    return x.v < y.v;
}

int main()
{
    int n, last, sum1, sum2;
    while(scanf("%d", &n) != EOF)
    {
        sum1 = 0;
        sum2 = 0;
        memset(ans, 0, sizeof(ans));
        for(int i = 1; i <= 2 * n; i++)
        {
            scanf("%d", &a[i].v);
            a[i].id = i;
        }
        sort(a + 1, a + 2 * n + 1, cmp);
        last = 2;
        for(int i = 3; i <= 2 * n; i++)
        {
            if(a[i].v == a[i - 1].v && a[i].v == a[i - 2].v)
                if(last == 2)
                {
                    ans[a[i].id] = 1;
                    last = 1;
                }
                else
                {
                    ans[a[i].id] = 2;
                    last = 2;
                }
        }
        for(int i = 1; i <= 2 * n; i++)
        {
            if(ans[a[i].id] == 0)
            {
                if(last == 1)
                {
                    ans[a[i].id] = 2;
                    sum2++;
                    last = 2;
                }
                else
                {
                    ans[a[i].id] = 1;
                    sum1++;
                    last = 1;
                }
            }
        }
        printf("%d\n", sum1 * sum2);
        printf("%d", ans[1]);
        for(int i = 2; i <= 2 * n; i++)
            printf(" %d", ans[i]);
        printf("\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值