1236. 递增三元组

题目链接

1236. 递增三元组

给定三个整数数组

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

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

1i,j,kN
Ai<Bj<Ck

输入格式

第一行包含一个整数 N

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

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

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

输出格式

一个整数表示答案。

数据范围

1N105,
0Ai,Bi,Ci105

输入样例:

3 1 1 1 2 2 2 3 3 3

输出样例:

27

解题思路

二分/双指针

可以枚举中间的 Bj,选出比 Bj 小的 Ai 且比 Bj 大的 Ck,可用二分或双指针找出其个数

  • 时间复杂度:
    二分: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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15885045.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示