HOJ2275 Number sequence

Number sequence

My Tags  (Edit)
  Source : SCU Programming Contest 2006 Final
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 1632, Accepted : 440

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

这题可以用树状数组做,开两个一维的树状数组分别记录当前点前面的比这点小的个数和后面比这点大的个数。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 50005
#define ll long long
int b1[maxn],b2[maxn],a[maxn];
int lowbit(int x){
	return x&(-x);
}
void update1(int pos,int num)
{
	while(pos<=maxn){
		b1[pos]+=num;pos+=lowbit(pos);
	}
}
int getsum1(int pos)
{
	int num=0;
	while(pos>0){
		num+=b1[pos];pos-=lowbit(pos);
	}
	return num;
}

void update2(int pos,int num)
{
	while(pos<=maxn){
		b2[pos]+=num;pos+=lowbit(pos);
	}
}
int getsum2(int pos)
{
	int num=0;
	while(pos>0){
		num+=b2[pos];pos-=lowbit(pos);
	}
	return num;
}

int main()
{
	int n,m,i,j;
	ll num=0;
	while(scanf("%d",&n)!=EOF)
	{
		memset(b1,0,sizeof(b1));
		memset(b2,0,sizeof(b2));
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			a[i]++;
			if(i==1){
				update1(a[i],1);continue;
			}
			update2(a[i],1);
		}
		num=0;
		for(i=2;i<=n-1;i++){
			num+=getsum1(a[i]-1)*getsum2(a[i]-1);
			update1(a[i],1);
			update2(a[i],-1);
		}
		printf("%lld\n",num);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值