hoj 2275 Number sequence
来源:互联网 发布:ps aux grep nginx 编辑:程序博客网 时间:2024/06/06 00:37
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 50010using 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;}
1 0
- HOJ 2275 Number Sequence
- hoj 2275 Number Sequence
- hoj 2275 Number sequence
- Hoj 2275 Number Sequence
- HOJ 2275 Number sequence
- hoj 2275 Number sequence 树状数组解法
- HOJ 2275 Number sequence(树状数组)
- hoj 2275 Number sequence(树状数组)
- HOJ 2275 Number sequence(树状数组)
- -----hoj 2275 Number sequence(树状数组)
- HOJ 2275——Number sequence(树状数组)
- HOJ 2275 Number sequence(树状数组) 大于小于问题
- 树状数组题目总结三( 上)( HOJ 2275 Number sequence )
- HIT 2275 Number sequence
- hoj Score Sequence DP
- HOJ 2761 Gibonacci Sequence
- HOJ 1603 Brackets Sequence
- HOJ MaoLaoDa Number
- 【Leetcode长征系列】Binary Tree Preorder Traversal
- zstack定位程序的学习--盲节点程序解读
- Android应用开发:搞定带标签滑动界面
- C语言堆栈入门——堆和栈的区别
- php验证名字验证email
- hoj 2275 Number sequence
- 字符串通配符匹配问题
- Ext.get()和Ext.getCmp()
- Fiddler软件 HTTP 请求和响应
- QT控件常用贴士。。。待续
- [MQ]关于ActiveMQ的配置
- Linux下的调试工具
- poj1328 贪心
- 亲自修改pe病毒加强版,感染后的文件可取代母体