AcWing 1236. 递增三元组
原题链接
中等
作者:
hegehog
,
2020-07-08 22:04:16
,
所有人可见
,
阅读 537
C++代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int a[N], b[N], c[N];
int 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];
int cnt[N], sa[N], sc[N];
//求 a 每个数值个数的前缀和
for(int i = 1; i <= n; i ++ )
cnt[a[i]] ++;
sa[0] = cnt[0];
for(int i = 1; i <= N; i ++ )
sa[i] = sa[i-1] + cnt[i];
//求 c 每个数值个数的前缀和
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= n; i ++ )
cnt[c[i]] ++;
sc[0] = cnt[0];
for(int i = 1; i <= N; i ++ )
sc[i] = sc[i-1] + cnt[i];
int t = sc[N];
ll res = 0;
for(int i = 1; i <= n; i ++ ){
res += ((ll)sa[ b[i]-1 ] * ( t - sc[b[i]] ));//注意数据范围 1e5*1e5 > int的范围
//cout << sa[b[i]-1] << " " << t << " " << res <<endl;
}
cout << res << endl;
return 0;
}
//暴力枚举(不重不漏):三重循环判断 Ai < Bj < Ck
//时间复杂度:O(n^3)