def crowd_comparison(p, q):
    """Crowded Comparison Operator. Return 1 means p is better than q.
    Return -1 means q is better than p.
    """
    if p.rank < q.rank or (p.rank == q.rank and p.distance > q.distance):
        return 1
    elif p.rank > q.rank or (p.rank == q.rank and p.distance < q.distance):
        return -1
    else:
        return 0