前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hoj 2275 Number sequence

hoj 2275 Number sequence

作者头像
全栈程序员站长
发布2022-07-06 10:21:09
1500
发布2022-07-06 10:21:09
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

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

题目就是统计序列中Ai < Aj > Ak(i < j < k)的个数。能够从前往后统计每一个元素之前小于它的数的个数,在从后往前统计每一个元素之后小于它的数的个数。然后乘积加和就可以。用树状数组轻松搞定!

AC代码例如以下:

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
#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 (a<M)
    {
        c[a]+=b;
        a+=lowbit (a);
    }
}

int sum(int a)
{
    int ans=0;
    while(a>0)
    {
        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\n",ans);
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117267.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月8,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Number sequence
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档