大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
Number sequence
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 longint 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;}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116715.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...