Hdu 4000 树状数组
2012-08-25 17:05
218 查看
Fruit Ninja
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 19 Accepted Submission(s) : 11
[align=left]Problem Description[/align]
Recently, dobby is addicted in the Fruit Ninja. As you know, dobby is a free elf, so unlike other elves, he could do whatever he wants.But the hands of the elves are somehow strange, so when he cuts the fruit, he can only make specific
move of his hands. Moreover, he can only start his hand in point A, and then move to point B,then move to point C,and he must make sure that point A is the lowest, point B is the highest, and point C is in the middle. Another elf, Kreacher, is not interested
in cutting fruits, but he is very interested in numbers.Now, he wonders, give you a permutation of 1 to N, how many triples that makes such a relationship can you find ? That is , how many (x,y,z) can you find such that x < z < y ?
[align=left]Input[/align]
The first line contains a positive integer T(T <= 10), indicates the number of test cases.For each test case, the first line of input is a positive integer N(N <= 100,000), and the second line is a permutation of 1 to N.
[align=left]Output[/align]
For each test case, ouput the number of triples as the sample below, you just need to output the result mod 100000007.
[align=left]Sample Input[/align]
2 6 1 3 2 6 5 4 5 3 5 2 4 1
[align=left]Sample Output[/align]
Case #1: 10 Case #2: 1 判断满足i<j<k且num[i]<num[k]<num[j]的总组数利用树状数组可以求出一个数前面比它小的数的个数,进而可以知道前面比它大的数的个数,总的比它大的个数减去前面比它大的个数等于后面比它大的个数,所以答案就很明显了~代码:[code]#include<stdio.h> #include<string.h> int c[100010]; int n; int lowbit(int x) { return x&(-x); } void update(int x,int d) { while(x<=n) { c[x]+=d; x+=lowbit(x); } } int sum(int x) { int ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans; } int main() { __int64 ans; int t,cases=1; int i,a; scanf("%d",&t); while(t--) { ans=0; memset(c,0,sizeof(c)); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a); update(a,1); int presmaller=sum(a-1); int totbigger=n-a; int prebigger=i-sum(a-1)-1; __int64 afterbigger=totbigger-prebigger; ans-=presmaller*afterbigger; if(afterbigger>=2) ans+=afterbigger*(afterbigger-1)/2; } printf("Case #%d: %I64d\n",cases++,ans%100000007); } return 0; }
[/code]
相关文章推荐
- hdu 4000 树状数组
- hdu 4000 树状数组
- hdu 4000Fruit Ninja 树状数组
- hdu 4000 Fruit Ninja 树状数组
- hdu 4000 树状数组
- hdu 4000 树状数组
- hdu 4000 树状数组
- hdu 4417 Super Mario(树状数组+离线处理)
- hdu 4358 Boring counting 树状数组
- HDU 4417 Super Mario(12年杭州网络赛-H题-离线 + 树状数组)
- HDU 2852 (树状数组+无序第K小)
- HDU 2642 Stars(二维树状数组)
- HDU 3584 树状数组
- HDU 5892 Resident Evil(状态压缩+树状数组)
- hdu 3333 树状数组
- hdu 5465(二维树状数组)
- HDU 3584 Cube (三维树状数组)
- HDU - 5699(79/60 4000 0)
- hdu 1556 Color the ball 树状数组
- hdu 4638 Group 树状数组