The ideal free distribution when competitive abilities differ: an approach based on statistical mechanics