zoukankan      html  css  js  c++  java
  • [Codeforces 459D] Pashmak and Parmida's problem

    [题目链接]

             https://codeforces.com/contest/459/problem/D

    [算法]

              首先用std :: map预处理 f(1, i, ai)和f(j, n, aj)

              然后用树状数组计算合法二元组对数 , 即可

              时间复杂度 : O(NlogN)

    [代码]

              

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e6 + 10;
    
    int n;
    int a[MAXN],va[MAXN],vb[MAXN],c[MAXN];
    long long ans;
    map<int,int> mp;
    
    inline int lowbit(int x)
    {
            return x & (-x); 
    }
    inline void add(int pos)
    {
            for (int i = pos; i <= n; i += lowbit(i)) c[i]++;
    }        
    inline int query(int pos)
    {
            int ret = 0;
            for (int i = pos; i >= 1; i -= lowbit(i)) ret += c[i];
            return ret;
    }
    
    template <typename T> inline void read(T &x)
    {
        T f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    
    int main()
    {
            
            read(n);
            for (int i = 1; i <= n; i++) read(a[i]);
            for (int i = 1; i <= n; i++) va[i] = ++mp[a[i]];
            mp.clear();
            for (int i = n; i >= 1; i--) vb[i] = ++mp[a[i]];
            for (int i = n; i >= 1; i--)
            {
                    ans += query(va[i] - 1);    
                    add(vb[i]);
            }
            printf("%I64d
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    spring 装配核心笔记
    小明种苹果
    线性分类器
    报数
    编程计划2.0 //不断更新
    MySQL基础之存储过程
    MySQL基础之视图
    指令系统.传送类指令
    MySQL基础之索引
    寻址方式总结
  • 原文地址:https://www.cnblogs.com/evenbao/p/9715877.html
Copyright © 2011-2022 走看看