hoj 2275 Number sequence

hoj 2275 Number sequence

大家好,又见面了,我是全栈君,今天给大家准备了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

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


AC代码例如以下:


#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.net/117267.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • golang激活码【2021.10最新】

    (golang激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~92U4ILM59M-eyJsaWNlb…

    2022年3月29日
    92
  • 怎么完全卸载赛门铁克_如何卸载symantec

    怎么完全卸载赛门铁克_如何卸载symantec前段时间,业务的虚机上安装了symantecEndpointProtection(正版),发现虚机运行一段时间就会失去响应死机,并且有些安装symantec的虚机3389端口无法使用,怎么折腾都不行。最后决定卸载它。一、是否可以用停止服务和终止进程再卸载的方式卸载呢?答案不行有3个symantec的服务,前2个可以终止,但symantecEndpointProtection这个服务打开都…

    2022年6月12日
    82
  • vue混合app开发框架_mpvue框架

    vue混合app开发框架_mpvue框架1.npm安装npminstallflyio–save.2.src下新建utils/request.js文件importFlyfrom’flyio/dist/npm/wx’constfly=newFly()consthost=’https://rmall.ukelink.net’//添加请求拦截器fly.interceptors.request.use…

    2022年9月8日
    0
  • mac navicat premium15激活码【永久激活】[通俗易懂]

    (mac navicat premium15激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    168
  • 网络基础知识题_无基础最易入门乐器

    网络基础知识题_无基础最易入门乐器本文结合多年来的工作实践,来详细讲述一下作为IT从业人员要掌握的一些基础网络知识。

    2025年6月12日
    0
  • Ubuntu18.04搭建源码搜索引擎Opengrok

    Ubuntu18.04搭建源码搜索引擎OpengrokTableofContents1OpenGrok介绍2安装OpenGrok2.1安装JAVA运行环境2.2安装Web服务器-Tomcat2.3安装OpenGrok2.4配置OpenGrok2.5安装 universal-ctags2.6建立源码索引2.6更新源码索引1OpenGrok介绍OpenGrok isafastand…

    2022年4月29日
    76

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号