牛客练习赛7 E 珂朵莉的数列

珂朵莉的数列

思路:

树状数组+高精度

离散化不知道哪里写错了,一直wa,最后用二分写的离散化

哪位路过大神可以帮我看看原来的那个离散化错在哪里啊

通过代码:

import java.math.BigInteger;
import java.util.*;
import java.util.Scanner;
  
class node
{
    int x;
    int id;
}
class cmp implements Comparator
{
    public int compare(node A,node B)
    {
        if(A.xreturn -1;
        else if(A.x==B.x)return 0;
        else return 1;
    }
}
public class Main{
    public final static int N=(int)1e6+9;
    public static int n;
    public static int a[]=new int[N];
    public static long b[]=new long[N];
    public static long bit[]=new long[N];
    public static long sum(int x)
    {
        long ret=0;
        while(x>0)
        {
            ret+=bit[x];
            x-=x&(-x);
        }
        return ret;
    }
    public static void add(int x,long a)
    {
        while(x<=n)
        {
            bit[x]+=a;
            x+=x&(-x);
        }
    }
    public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            n=reader.nextInt();
            for(int i=1;i<=n;i++)
            {
                a[i]=reader.nextInt();
                b[i]=a[i];
            }
            Arrays.sort(b,1,n+1);
            for(int i = 1; i <= n; i++) {
                a[i] = Arrays.binarySearch(b, 1 ,n + 1,a[i]);
            }
            BigInteger ans=BigInteger.valueOf(0);
            for(int i=1;i<=n;i++)
            {
                ans=ans.add(BigInteger.valueOf((sum(n)-sum(a[i]))*(n-i+1)));
                add(a[i],i);
            }
            System.out.println(ans);
        }
}

错误代码:

import java.math.BigInteger;
import java.util.*;
import java.util.Scanner;
  
class node
{
    int x;
    int id;
}
class cmp implements Comparator
{
    public int compare(node A,node B)
    {
        if(A.xreturn -1;
        else if(A.x==B.x)return 0;
        else return 1;
    }
}
public class Main{
    public final static int N=(int)1e6+9;
    public static int n;
    public static node a[]=new node[N];
    public static int b[]=new int[N];
    public static long bit[]=new long[N];
    public static long sum(int x)
    {
        long ret=0;
        while(x>0)
        {
            ret+=bit[x];
            x-=x&(-x);
        }
        return ret;
    }
    public static void add(int x,long a)
    {
        while(x<=n)
        {
            bit[x]+=a;
            x+=x&(-x);
        }
    }
    public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            n=reader.nextInt();
            for(int i=1;i<=n;i++)
            {
                a[i]=new node();
                a[i].x=reader.nextInt();
                a[i].id=i;
            }
            //
            Arrays.sort(a,1,n+1,new cmp());
              
            int cnt=1;
            a[0]=new node();
            a[0].x=-1;
            for(int i=1;i<=n;i++)
            {
                if(a[i].x!=a[i-1].x)b[a[i].id]=++cnt;
                else b[a[i].id]=cnt;
            }
            //for(int i=1;i<=n;i++)System.out.println(b[i]);
            BigInteger ans=BigInteger.valueOf(0);
            for(int i=1;i<=n;i++)
            {
                ans=ans.add(BigInteger.valueOf((sum(cnt)-sum(b[i]))*(n-i+1)));
                add(b[i],i);
            }
            System.out.println(ans);
        }
}

 

转载于:https://www.cnblogs.com/widsom/p/7955793.html

你可能感兴趣的:(牛客练习赛7 E 珂朵莉的数列)