HOJ 2275——Number sequence(树状数组)
2014-08-28 20:22
141 查看
Number sequence
My Tags | (Edit) |
---|
Source : SCU Programming Contest 2006 Final | |||
Time limit : 1 sec | Memory limit : 64 M |
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
——————————————————分割线——————————————————
题目大意:
给定n个数,求满足Ai < Aj > Ak and i < j < k.的总的组合数
思路:
对于第i个数,顺序统计比它小的数的个数a,再逆序统计比它小的个数b,就能得到左右两边比它小的数的个数,那么对于第i个数的方案数就是a*b
还有这个题目没说EOF结束的,但是如果没有EOF就错了,错了好几发~~~
#include<iostream> #include<cstring> #include<cstdio> #define Local #define M 50000+10 #define maxn 32768 #define ll long long using namespace std; int a[M],b[M],c[M]; void modify(int x,int v) { for(int i=x;i<=M;i+=i&-i){ c[i]+=v; } } int getsum(int x) { int sum=0; for(int i=x;i>0;i-=i&-i){ sum+=c[i]; } return sum; } int main() { #ifdef local freopen("in.txt","r",stdin); freopen("ou.txt","w",stdout); #endif // local int n; while(scanf("%d",&n)!=EOF){ memset(c,0,sizeof(c)); ll sum=0; for(int i=0;i<n;++i){ scanf("%d",&a[i]); a[i]++; b[i]=getsum(a[i]-1); modify(a[i],1); } memset(c,0,sizeof(c)); for(int i=n-1;i>=0;--i){ sum+=(ll)getsum(a[i]-1)*b[i]; modify(a[i],1); } printf("%lld\n",sum); } return 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 树状数组 水··不能int 64
- hoj (2275)树状数组
- [hoj 2678]Stars[二维树状数组]
- hoj 1867 经理的烦恼 (树状数组)
- hoj 2430 Counting the algorithms(树状数组,贪心)
- HOJ 1867 经理的烦恼 【 树状数组 】
- HOJ——T 2275 Number sequence
- HOJ 2275 Number Sequence
- HOJ 1016 Joseph's problem I 树状数组求第K大
- hoj 2430 Counting the algorithms (树状数组)
- Hoj 2275 Number Sequence
- hoj 2275 Number Sequence
- [树状数组]HOJ 10069 星星的等级
- hoj 2430 树状数组 简单题