题目链接:http://codeforces.com/contest/353/problem/B
题意:给出2n2n个两位数,现在要将其分成两个集合,每个集合nn个数,分好集合后每次可以从两个集合分别取出一个数字组成一个四位数,问如果分可以使得组成的数字种类最多
思路:对于数量超过11的数字,先给每个集合一个,对于数量为11的数字尽量等分到两个集合,剩下的数字随便给
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f, maxn = 205;
int n, a[maxn], ans[maxn], num[maxn], vis[maxn];
int main()
{
while (~scanf("%d", &n))
{
memset(num, 0, sizeof(num));
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= 2 * n; i++)
{
scanf("%d", &a[i]);
num[a[i]]++;
}
int num0 = 0, num1 = 0, num2 = 0;
for (int i = 1; i <= 2 * n; i++)
{
if (num[a[i]] >= 2)
{
if (!vis[a[i]])num0++, ans[i] = 1, vis[a[i]] = 1;
else if (vis[a[i]] == 1)ans[i] = 2, vis[a[i]] = 2;
}
else
{
if (num1 > num2)ans[i] = 2, num2++;
else ans[i] = 1, num1++;
}
}
num1 += num0, num2 += num0;
printf("%d
", num1 * num2);
for (int i = 1; i <= 2 * n; i++)
if (vis[a[i]] == 2 && !ans[i])
{
if (num1 < n)ans[i] = 1, num1++;
else ans[i] = 2;
}
for (int i = 1; i <= 2 * n; i++)printf("%d%c", ans[i], i == 2 * n ? '
' : ' ');
}
return 0;
}