1、ASC
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
scan.close();
}
}
2、递增三元组
题目描述
给定三个整数数组
A=[A1,A2,⋯AN],
B=[B1,B2,⋯BN],
C=[C1,C2,⋯CN],
请你统计有多少个三元组 (i,j,k) 满足:
-
1≤i,j,k≤N;
-
Ai<Bj<Ck。
输入描述
第一行包含一个整数 N。
第二行包含 N 个整数 A1,A2,⋯AN。
第三行包含 N 个整数 B1,B2,⋯BN。
第四行包含 N 个整数 C1,C2,⋯CN。
其中,1≤N≤105,0≤Ai,Bi,Ci≤105。
输出描述
输出一个整数表示答案。
输入输出样例
示例
输入
3
1 1 1
2 2 2
3 3 3
输出
27
运行限制
- 最大运行时间:2s
- 最大运行内存: 256M
import java.util.*;
import java.io.*;
public class 递增三元组 {
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 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter (new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException {
String[] s = in.readLine().split(" ");
int n = Integer.parseInt(s[0]);
s= in.readLine().split(" ");
for(int i=0;i<n;i++) {
a[i]=Integer.parseInt(s[i]);
}
s=in.readLine().split(" ");
for(int i=0;i<n;i++) {
b[i]=Integer.parseInt(s[i]);
}
s=in.readLine().split(" ");
for(int i=0;i<n;i++) {
c[i]=Integer.parseInt(s[i]);
}
Arrays.sort(a,0,n);
Arrays.sort(b,0,n);
Arrays.sort(c,0,n);
long res = 0L;
for(int i =0;i<n;i++) {//每次以中间数组B为中心,从A数组找到比B数组小的(找到最后一个满足的),从C数组中找到比B数组大的(找到第一个满足的),从而进行匹配
int l = 0;
int r = n-1;
while(l<r) {
int mid=l+r+1>>1;//目标在右边方向
if(a[mid]<b[i]) {
l = mid;
}else {
r = mid-1;
}
}
int BA = l;
l = 0;
r = n-1;
while(l<r) {
int mid = l+r>>1;//目标在左边方向
if(c[mid]>b[i]) {
r = mid;
}else {
l = mid+1;
}
}
int CB = l;
// res+=(long)(BA+1)*(n-CB);
if(a[BA]<b[i]&&c[CB]>b[i]) {
res+=(long)(BA+1)*(n-CB);
}
}
System.out.println(res);
}
}