• codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)


    题目链接:

    D. Pashmak and Parmida's problem

    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

    There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r andak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).

    Help Pashmak with the test.

    Input

    The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109).

    Output

    Print a single integer — the answer to the problem.

    Examples
    input
    7
    1 2 1 1 2 2 1
    output
    8
    input
    3
    1 1 1
    output
    1
    input
    5
    1 2 3 4 5
    output
    0
    题意:f[l,r,x]=在a[l],a[l+1]...a[r]中有多少个a[i]等于x,这道题可以问f[1,i,a[i]]>f[j,n,a[j]]&&1<=i<j<=n的i和j的对数,令pre[i]为a[i]在区间[1,i]中出现的次数,同理nex[j]为a[j]为区间[j,n]中a[j]出现的次数,结果就变成了求sigma{pre[i]和nex[i+1]的逆序对数}i为1到n-1;这时不是单单的一个数组求逆序对数(一个数组求逆序对数可以在归并排序中解决),所以得用线段树或者树状数组,树状数组还不会,等学会了树状数组再来更树状数组的代码;
    AC代码:

    /*~~~~~~~~线段树的代码~~~~~~~~~~~~~*/
    #include <bits/stdc++.h> using namespace std; const int N=1e6+4; int n,a[N],pre[N],nex[N]; struct nod { int l,r,sum; }; nod tree[4*N]; void build(int node,int le,int ri) { tree[node].l=le; tree[node].r=ri; tree[node].sum=0; if(le==ri)return ; int mid=(le+ri)>>1; build(2*node,le,mid); build(2*node+1,mid+1,ri); tree[node].sum=tree[2*node].sum+tree[2*node+1].sum; } int query(int node,int L,int R) { if(L<=tree[node].l&&R>=tree[node].r) { return tree[node].sum; } int mid=(tree[node].l+tree[node].r)>>1; if(R<=mid)return query(2*node,L,R); else if(L>mid)return query(2*node+1,L,R); else return query(2*node,L,R)+query(2*node+1,L,R); } int update(int node,int num) { if(tree[node].l==tree[node].r&&tree[node].l==num) { tree[node].sum+=1; return 0; } int mid=(tree[node].l+tree[node].r)>>1; if(num<=mid)update(2*node,num); else update(2*node+1,num); tree[node].sum=tree[2*node].sum+tree[2*node+1].sum; } map<int,int>mp1,mp2; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); mp1[a[i]]++; pre[i]=mp1[a[i]];//用map进行离散化 } for(int i=n;i>0;i--) { mp2[a[i]]++; nex[i]=mp2[a[i]]; } long long ans=0; build(1,1,n+1);//建树时建到n+1,避免后面的nex[i]+1>n; for(int i=1;i<=n;i++) { if(nex[i]!=n) ans+=(long long)query(1,nex[i]+1,n); update(1,pre[i]); } cout<<ans<<" "; return 0; }
    /*~~~~~~~~树状数组的代码~~~~~~~~~~为什么用树状数组还没有线段树的快?*/
    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e6+4;
    int n,a[N],pre[N],nex[N],sum[N];
    int lowbit(int x)
    {
        return x&(-x);
    }
    void update(int x)
    {
        while(x<=n)
        {
            sum[x]++;
            x+=lowbit(x);
        }
    }
    int query(int x)
    {
        int s=0;
        while(x>0)
        {
            s+=sum[x];
            x-=lowbit(x);
        }
        return s;
    }
    map<int,int>mp1,mp2;
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            mp1[a[i]]++;
            pre[i]=mp1[a[i]];
        }
        for(int i=n;i>0;i--)
        {
            mp2[a[i]]++;
            nex[i]=mp2[a[i]];
        }
        long long ans=0;
        for(int i=n;i>0;i--)
        {
            ans+=(long long)query(pre[i]-1);
            update(nex[i]);
        }
        cout<<ans<<"
    ";
        return 0;
    }


  • 相关阅读:
    ACM-生化武器
    ACM-Antiprime数
    ACM-寻宝
    ACM-小偷的背包
    ACM-吴奶奶买鱼
    ACM-挑战题之排列生成
    ACM-数细胞
    ACM-售货员难题
    学习《linux》课程
    MATLAB 求圆形面积
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5313177.html
Copyright © 2020-2023  润新知