HOJ2275 Number sequence

HOJ2275 Number sequence

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Number sequence

My Tags Edit)
  Source :    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 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;}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

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


相关推荐

  • 编程之美2: 二进制重建

    编程之美2: 二进制重建

    2022年1月6日
    48
  • centos7 安装图形界面

    centos7 安装图形界面本人用VMware版本15.0.2build-10952284(关系不大)开启前要在虚拟机–>设置–>3D图形如下如果是云服务器跳过此步yum下载图形界面软件yumgroupinstall”GNOMEDesktop””GraphicalAdministrationTools”修改配置ln-sf/lib/systemd/system…

    2022年5月16日
    41
  • 哈夫曼树

    哈夫曼树一、哈夫曼树的概念和定义 什么是哈夫曼树?让我们先举一个例子。判定树:       在很多问题的处理过程中,需要进行大量的条件判断,这些判断结构的设计直接影响着程序的执行效率。例如,编制一个程序,将百分制转换成五个等级输出。大家可能认为这个程序很简单,并且很快就可以用下列形式编写出来:  if(score<60) cout<<"Bad"<

    2022年6月11日
    34
  • 微软ASP.NET网站部署指南(10):迁移至SQL Server[通俗易懂]

    微软ASP.NET网站部署指南(10):迁移至SQL Server

    2022年2月2日
    42
  • VCLibs库文件包下载_怎么使用已有的加速包

    VCLibs库文件包下载_怎么使用已有的加速包本文以MDI应用程序为例说明如何在已有的VC++工程中使用BCG界面库,我的开发环境为VS2003。1、将BCG/BCGCBPro目录路径添加到“项目属性->C/C++->常规->附加包含目录”中,同时将BCG/Bin目录路径添加到“项目属性->链接器->常规->附加库目录”中。2、确保在CWinApp派生类(设为CMyApp)的InitInstance()成员函数中调用AfxOl

    2022年10月8日
    2
  • Windows 定时关机命令

    Windows 定时关机命令1.定时关机1、windows+R弹出命令框,输入cmd,并确定2、输入shutdown-s-t3600时,1小时之后关机,最后一个代表的是多少秒后关机,3600秒为1小时,当为0时,立刻关机,当你按下回车键时,右下角会弹出提示——你的计算机将在多久以后关闭。2.其他命令序号命令作用1shutdown-s-t0s:表示shutdown,关机;t:表示time,关机时间,单位秒;0:表示关机时间,立刻关机;2shutdow

    2022年5月15日
    75

发表回复

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

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