1 条题解

  • 0
    @ 2026-9-30 12:33:32

    C++ :

    
    #include <cstdio>
    /* 值三元组 (x,y,y) 且 x != y:
       合法 y 必需出现 >= 2 次;此时取 y 的「首次出现位置」j 最优(i 的可选范围最大),
       而 x 可以是 j 之前的任意「不同值」。
       所以答案 = Σ_{cnt(y)>=2} ( y 首次出现之前的不同值个数 )。 */
    static const int MAXA = 1000005;
    static int seen[MAXA], cnt[MAXA], dcAt[MAXA];
    int main(){
        int n;
        if(scanf("%d", &n) != 1) return 0;
        long long ans = 0;
        int dc = 0;
        for(int i = 0; i < n; i++){
            int x;
            if(scanf("%d", &x) != 1) break;
            if(!seen[x]){
                seen[x] = 1;
                dcAt[x] = dc;              /* 之前的「不同值」个数 */
                dc++;
            }
            cnt[x]++;
            if(cnt[x] == 2) ans += dcAt[x];
        }
        printf("%lld\n", ans);
        return 0;
    }
    
    
    • 1

    信息

    ID
    2584
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者