1236. 递增三元组

题目链接

1236. 递增三元组

给定三个整数数组

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

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

\(1≤i,j,k≤N\)
\(A_i<B_j<C_k\)

输入格式

第一行包含一个整数 \(N\)

第二行包含 \(N\) 个整数 \(A_1,A_2,…A_N\)

第三行包含 \(N\) 个整数 \(B_1,B_2,…B_N\)

第四行包含 \(N\) 个整数 \(C_1,C_2,…C_N\)

输出格式

一个整数表示答案。

数据范围

\(1≤N≤10^5,\)
\(0≤A_i,B_i,C_i≤10^5\)

输入样例:

3
1 1 1
2 2 2
3 3 3

输出样例:

27

解题思路

二分/双指针

可以枚举中间的 \(B_j\),选出比 \(B_j\) 小的 \(A_i\) 且比 \(B_j\) 大的 \(C_k\),可用二分或双指针找出其个数

  • 时间复杂度:
    二分:\(O(nlogn)\)
    双指针:\(O(n)\)

代码

  • 二分
// Problem: 递增三元组
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/1238/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e5+5;
int a[N],b[N],c[N],n;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<=n;i++)cin>>c[i];
    sort(a+1,a+1+n);
    sort(b+1,b+1+n);
    sort(c+1,c+1+n);
    LL res=0;
	for(int i=1;i<=n;i++)
	{
		int pos1=upper_bound(c+1,c+1+n,b[i])-c;
		int pos2=lower_bound(a+1,a+1+n,b[i])-a-1;
		if(pos1<=n&&pos2>=1)res+=1ll*pos2*(n-pos1+1);	
	}
	cout<<res;
    return 0;
}
  • 双指针
// Problem: 递增三元组
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/1238/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e5+5;
int a[N],b[N],c[N],n;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<=n;i++)cin>>c[i];
    sort(a+1,a+1+n);
    sort(b+1,b+1+n);
    sort(c+1,c+1+n);
    LL res=0;
	for(int i=1;i<=n;i++)
	{
		int pos1=upper_bound(c+1,c+1+n,b[i])-c;
		int pos2=lower_bound(a+1,a+1+n,b[i])-a-1;
		if(pos1<=n&&pos2>=1)res+=1ll*pos2*(n-pos1+1);	
	}
	cout<<res;
    return 0;
}
posted @ 2022-02-11 22:38  zyy2001  阅读(36)  评论(0编辑  收藏  举报