Educational Codeforces Round 3 B. The Best Gift

链接: https://codeforces.com/contest/609/problem/B

public class TaskB {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.readInt();
        int m = in.readInt();
        int[] ary = in.readIntArray(n);
        int[] count = new int[m];
        long res = 0;
        for (int i = 0; i < ary.length; i++) {
            count[ary[i]-1]++;
        }
        for (int i = 0; i < m; i++) {
            for (int j = i+1; j < m; j++) {
                res += count[i]*count[j];
            }
        }
        out.print(res);
    }
}

就是普通的排序问题, 从m个不同类的物品中, 抽取(不重复)2个不同的物品. 物品没有先后顺序.

第一个循环计算每种物品的个数, 第二个循环外边遍历每种物品, 里面遍历当前物品外的物品, 中间因为物品没有顺序所以是乘法