您的位置:首页 > 产品设计 > UI/UE

HOJ——T 2275 Number sequence

2017-08-19 15:33 246 查看

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多组数据 )
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

const int N(50000+5);
#define LL long long
int an1
,an2
;
int n,a
,tr
;

#define lowbit(x) (x&((~x)+1))
inline void Update(int i,int x)
{
for(;i<=32770;i+=lowbit(i)) tr[i]+=x;
}
inline int Query(int x)
{
int ret=0;
for(;x;x-=lowbit(x)) ret+=tr[x];
return ret;
}

int main()
{
for(LL ans=0;~scanf("%d",&n);ans=0)
{
for(int i=1;i<=n;i++) scanf("%d",a+i);
memset(tr,0,sizeof(tr));
for(int i=1;i<=n;i++)
an1[i]=Query(a[i]-1),Update(a[i],1);
memset(tr,0,sizeof(tr));
for(int i=n;i>=1;i--)
an2[i]=Query(a[i]-1),Update(a[i],1);
for(int i=1;i<=n;i++) ans=ans+(LL)an1[i]*an2[i];
printf("%lld\n",ans);
}
return 0;
}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: