http://acm.hit.edu.cn/hoj/problem/view?id=2275
Source : SCU Programming Contest 2006 Final | |||
Time limit : 1 sec | Memory limit : 64 M |
Submitted : 1864, Accepted : 498
Given a number sequence which has N element(s), please calculate the number of different collocation for three number Ai, Aj, Ak, which satisfy that Ai < Aj > Ak and i < j < k.
InputThe first line is an integer N (N <= 50000). The second line contains N integer(s): A1, A2, ..., An(0 <= Ai <= 32768).
OutputThere is only one number, which is the the number of different collocation.
Sample Input5 1 2 3 4 1Sample Output
6
对于每个数求出两侧的小于当前数的数量,乘法原理求和
(woc开longlong mmp多组数据 )
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int N(50000+5); 8 #define LL long long 9 int an1[N],an2[N]; 10 int n,a[N],tr[N]; 11 12 #define lowbit(x) (x&((~x)+1)) 13 inline void Update(int i,int x) 14 { 15 for(;i<=32770;i+=lowbit(i)) tr[i]+=x; 16 } 17 inline int Query(int x) 18 { 19 int ret=0; 20 for(;x;x-=lowbit(x)) ret+=tr[x]; 21 return ret; 22 } 23 24 int main() 25 { 26 for(LL ans=0;~scanf("%d",&n);ans=0) 27 { 28 for(int i=1;i<=n;i++) scanf("%d",a+i); 29 memset(tr,0,sizeof(tr)); 30 for(int i=1;i<=n;i++) 31 an1[i]=Query(a[i]-1),Update(a[i],1); 32 memset(tr,0,sizeof(tr)); 33 for(int i=n;i>=1;i--) 34 an2[i]=Query(a[i]-1),Update(a[i],1); 35 for(int i=1;i<=n;i++) ans=ans+(LL)an1[i]*an2[i]; 36 printf("%lld ",ans); 37 } 38 return 0; 39 }