一题多解:递增三元组

给定三个整数数组

A=[A1,A2,…AN],
B=[B1,B2,…BN],
C=[C1,C2,…CN],

请你统计有多少个三元组 (i,j,k)

满足:

  1. 1≤i,j,k≤N
  • Ai<Bj<Ck

输入格式

第一行包含一个整数 N。

第二行包含 N个整数 A1,A2,…AN。

第三行包含 N个整数 B1,B2,…BN。

第四行包含 N个整数 C1,C2,…CN。

输出格式

一个整数表示答案。

数据范围

1 ≤ N ≤ 10^5,
0 ≤ Ai,Bi,Ci ≤ 10^5

输入样例:

3
1 1 1
2 2 2
3 3 3

输出样例:

27

方法一:暴力(62分)

#include<bits/stdc++.h>
#include<map>
#define endl '\n'
#define inf 0x3f3f3f3f

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

int a[N],b[N],c[N];

int cnt;

int main()
{   
    int n;
    scanf("%d",&n); 

    for(int i = 1 ; i <= n ; i ++)  scanf("%d",&a[i]);
    for(int i = 1 ; i <= n ; i ++)  scanf("%d",&b[i]);
    for(int i = 1 ; i <= n ; i ++)  scanf("%d",&c[i]);

    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = 1 ; j <= n ; j ++)
        {
            for(int k = 1 ; k <= n ; k ++)
            {
                if(a[i] < b[j] && b[j] < c[k] && a[i] < c[k])  cnt ++;
            }
        }
    }

    printf("%d\n",cnt);

    return 0; 
}

方法二:前缀和

import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = 100010;
    static int[] a = new int[N];
    static int[] b = new int[N];
    static int[] c = new int[N];
    static int[] acnt = new int[N]; //acnt[i]表示在A中i这个值出现多少次
    static int[] ccnt = new int[N]; //ccnt[i]表示在C中i这个值出现多少次
    static int[] s = new int[N]; //求从1到i中,所有数值为i的数出现的次数的前缀和
    static int[] as = new int[N]; //记录在A中由多少个数小于B[i]
    static int[] cs = new int[N]; //记录在C中由多少个数大于B[i]

    public static void main(String[] args) throws NumberFormatException, IOException
    {
        int n = rd.nextInt();
        // 把每个数都加1,不影响结果,对于as[i] = s[b[i] - 1]这个语句,如果b[i]等于0,就会-1越界,太寄吧巧妙了;
        for(int i = 1;i <= n;i++) a[i] = rd.nextInt() + 1;
        for(int i = 1;i <= n;i++) b[i] = rd.nextInt() + 1;
        for(int i = 1;i <= n;i++) c[i] = rd.nextInt() + 1;

        //求as[]
        for(int i = 1;i <= n;i++) acnt[a[i]] ++;
        for(int i = 1;i <= N - 1;i++) s[i] = s[i - 1] + acnt[i];
        for(int i = 1;i <= n;i++) as[i] = s[b[i] - 1];

        //求cs[]
        for(int i = 1;i <= n;i++) ccnt[c[i]] ++;
        for(int i = 1;i <= N - 1;i++) s[i] = s[i - 1] + ccnt[i];
        for(int i = 1;i <= n;i++) cs[i] = s[N - 1] - s[b[i]];

        //枚举每个b[i]
        long res = 0;
        for(int i = 1;i <= n;i++) res += (long)as[i] * cs[i];
        pw.println(res);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class math
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }

    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }

    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }

        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());

        // 对因子排序(升序)
        Collections.sort(a);

        return a;
    }

    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

方法三:二分

import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static math math_bag = new math();
    static int N = (int)1e5 + 10;
    static int a[] = new int[N];
    static int b[] = new int[N];
    static int c[] = new int[N];
 
    static int n;
 
    // 二分的绝妙板子,闭区间寻找
    // 寻找左端点,
    static int findleft(int l, int r, int x)
    {
        while(l <= r)
        {
            int mid = l + r >> 1;
            if(a[mid] == x)  r = mid - 1;
            else if(a[mid] > x)  r = mid - 1;
            else l = mid + 1;
        }
        return r + 1;  // 找到的时候,r = mid - 1被多执行一次,所以要return r + 1;
    }
 
    // 寻找右端点
    static int findright(int l ,int r, int x)
    {
        while(l <= r)
        {
            int mid = (l + r) / 2;
            if(c[mid] == x) l = mid + 1;
            else if(c[mid] > x) r = mid - 1;
            else l = mid + 1;
        }
        return l - 1;  // 找到的时候,l = mid + 1被多执行一次,所以要return l - 1;
    }
 
    public static void main(String[] args ) throws IOException, ParseException
    {
        n = rd.nextInt();
        for(int i = 0 ; i < n ; i ++)  a[i] = rd.nextInt();
        for(int i = 0 ; i < n ; i ++)  b[i] = rd.nextInt();
        for(int i = 0 ; i < n ; i ++)  c[i] = rd.nextInt();
 
        Arrays.sort(a,0,n);
        Arrays.sort(b,0,n);
        Arrays.sort(c,0,n);
 
        long res = 0;
        for(int i = 0 ; i < n ; i ++)
        {
            int left = findleft(0,n - 1,b[i]); 
            int right = findright(0,n - 1,b[i]);
 
            res += (long)(left) * (n - right - 1); // left表示小于b[i]的数量,n - right - 1表示大于b[i]的数量,依据乘法原理,每个b[i]能构成的逆序对的数量为(left) * (n - right - 1)
        }
        pw.println(res);
        pw.flush();
    }
}
 
class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
 
    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}
 
class math
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }
 
    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }
 
    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }
 
        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());
 
        // 对因子排序(升序)
        Collections.sort(a);
 
        return a;
    }
 
    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}
 
class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}
 
class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

方法四:双指针

#include<bits/stdc++.h>
#include<map>
#define endl '\n'
#define inf 0x3f3f3f3f

using namespace std;

typedef long long ll;

const int N = 1e5 + 10; 

int a[N],b[N],c[N]; 

int main()
{   
    int n;
    scanf("%d",&n); 

    for(int i = 0 ; i < n ; i ++)  scanf("%d",&a[i]); 
    for(int i = 0 ; i < n ; i ++)  scanf("%d",&b[i]);
    for(int i = 0 ; i < n ; i ++)  scanf("%d",&c[i]);

    sort(a, a + n);
    sort(b, b + n);
    sort(c, c + n);

    ll res = 0, l = 0, r = 0;
    for(int i = 0 ; i < n ; i ++)
    {
        while(a[l] < b[i] && l < n)  l ++;
        while(c[r] <= b[i] && r < n)  r ++;

        res += (ll)l * (n - r);  
    }

    printf("%lld\n",res);

    return 0; 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值