P8613 [蓝桥杯 2014 省 B] 小朋友排队

12 篇文章 0 订阅
10 篇文章 0 订阅


import java.math.BigInteger;
import java.util.*;

public class Main{
    static int n=100010;
    static int maxh=1000010;
    static long[] total=new long[n];
    static long ans;
    static int[] hight=new int[n];
    static int[] c1=new int[maxh];
    static int[] c2=new int[maxh];
    static int[] reverseOrder=new int[n];
    
    
public static void main(String[] args) {
    create();
    
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    
    for (int i=1;i<=n;i++)
    {
        hight[i]=sc.nextInt();
        add(hight[i]+1,1,c1);
        reverseOrder[i]=i-sum(hight[i]+1,c1);
    }
    
    for (int i=n;i>0;i--)
    {
        add(hight[i]+1,1,c2);
        reverseOrder[i]+=sum(hight[i],c2);
    }
    
    ans=0;
    for (int i=1;i<=n;i++) {
        ans+=total[reverseOrder[i]];
    }
    System.out.println(ans);
}

    public static int lowbit(int x)
    {
        return (x&(-x));
    }
    //打表
    public static void create()
    {
        total[0]=0;;
        for (int i=1;i<n;i++)
        {
            total[i]=total[i-1]+i;
        }
    }
    //单点更新
    public static void add(int pos,int num,int[] c)
    {
        while (pos<maxh)
        {
            c[pos]+=num;
            pos+=lowbit(pos);
        }
    }
    //求前pos位和
    public static int sum(int pos,int[] c)
    {
        int sum=0;
        while (pos>0)
        {
            sum+=c[pos];
            pos-=lowbit(pos);
        }
        return sum;
    }
    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值