hoj 2275 Number sequence
1430 ワード
Number sequence
Gven a number sequence which has N element(s)、please callate the number of different collection for three number Ai、Aj、Ak、which satisfy that Ai<Aj>Ak and i<j>Ak.i
Input
The first line is an integer N(N<=50000).The second line contains N integer(s):A 1,A 2,…,An(0==Ai<=32768).
Output
The e e e is only one number,which is the number of different collection.
Sample Input
51 2 3 4 1
Sample Output
6
テーマは統計シーケンスの中でAiです. Ak(i
ACコードは以下の通りです
Gven a number sequence which has N element(s)、please callate the number of different collection for three number Ai、Aj、Ak、which satisfy that Ai<Aj>Ak and i<j>Ak.i
Input
The first line is an integer N(N<=50000).The second line contains N integer(s):A 1,A 2,…,An(0==Ai<=32768).
Output
The e e e is only one number,which is the number of different collection.
Sample Input
51 2 3 4 1
Sample Output
6
テーマは統計シーケンスの中でAiです. Ak(i
ACコードは以下の通りです
#include
#include
#include
#define M 50010
using namespace std;
int c[M],num[M];
int l[M],n;
int lowbit(int a)
{
return a&-a;
}
void add(int a,int b)
{
while (a0)
{
ans+=c[a];
a-=lowbit(a);
}
return ans;
}
int main ()
{
int i,j;
int a,b;
while(~scanf("%d",&n))
{
memset(c,0,sizeof c);
memset(num,0,sizeof num);
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
l[i]=sum(num[i]-1);
add(num[i],1);
}
memset(c,0,sizeof c);
long long ans=0;
for(i=n;i>=1;i--)
{
ans=ans+(long long)sum(num[i]-1)*l[i];
add(num[i],1);
}
printf("%lld
",ans);
}
return 0;
}