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?
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.
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.
1 10 99
1 2 1
2 13 24 13 45
4 1 2 2 1
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");
}
}