HOJ——T 2275 Number sequence

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.

Input

The first line is an integer N (N <= 50000). The second line contains N integer(s): A1, A2, ..., An(0 <= Ai <= 32768).

Output

There is only one number, which is the the number of different collocation.

Sample Input
5
1 2 3 4 1
Sample 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\n",ans);
37     }
38     return 0;
39 }

 

 

转载于:https://www.cnblogs.com/Shy-key/p/7396766.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值