CF 253B Two Heaps
#include<stdio.h> #include<algorithm> #include<map> using namespace std; struct Node { int x; int number; int i; int flag; }a[300]; int b[300]; map<int,int>ma,mb1,mb2; bool cmp1(Node a,Node b) { if(a.i<b.i)return true; return false; } bool cmp2(Node a,Node b) { if(a.number<b.number)return true; if(a.number==b.number&&a.x<b.x)return true; return false; } int main() { int i,n; while(scanf("%d",&n)!=EOF) { ma.clear(); mb1.clear(); mb2.clear(); n=2*n; for(i=0;i<n;i++) { scanf("%d",&a[i].x); a[i].i=i; ma[a[i].x]++; //a[i].number=1; } for(i=0;i<n;i++) a[i].number=ma[a[i].x]; sort(a,a+n,cmp2);//number /*for(i=0;i<n;i++) printf("%d:%d ",a[i].x,a[i].number);*/ for(i=0;i<n;i+=2) { a[i].flag=1; mb1[a[i].x]; a[i+1].flag=2; mb2[a[i+1].x]; } sort(a,a+n,cmp1); printf("%d\n",mb1.size()*mb2.size()); for(i=0;i<n-1;i++) printf("%d ",a[i].flag); printf("%d\n",a[n-1].flag); } return 0; }