2018省賽第九屆藍橋杯真題C語言B組第六題題解 遞增三元組
阿新 • • 發佈:2019-02-16
標題:遞增三元組
給定三個整數陣列
A = [A1, A2, ... AN],
B = [B1, B2, ... BN],
C = [C1, C2, ... CN],
請你統計有多少個三元組(i, j, k) 滿足:
1. 1 <= i, j, k <= N
2. Ai < Bj < Ck
【輸入格式】
第一行包含一個整數N。
第二行包含N個整數A1, A2, ... AN。
第三行包含N個整數B1, B2, ... BN。
第四行包含N個整數C1, C2, ... CN。
對於30%的資料,1 <= N <= 100
對於60%的資料,1 <= N <= 1000
對於100%的資料,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000
【輸出格式】
一個整數表示答案
【樣例輸入】
3
1 1 1
2 2 2
3 3 3
【樣例輸出】
27
思路:二分,對於b陣列的每個數,我們去a數組裡找第一個比b[i]小的數的位置,去c數組裡找第一個比b[i]大的數的位置,這樣
2*Nlog(N)就能找到所有情況.
程式碼:
#include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mod 1000000007 using namespace std; typedef long long ll; const int maxn = 1e5+5; const double esp = 1e-7; const int ff = 0x3f3f3f3f; map<int,int>::iterator it; int n; int a[maxn],b[maxn],c[maxn]; int main() { cin>>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 ans = 0; for(int i = 0;i< n;i++) { int pos1 = lower_bound(a,a+n,b[i])-a; int pos2 = upper_bound(c,c+n,b[i])-c; ans+= (ll)pos1*(n-pos2); } cout<<ans<<endl; return 0; }