HDU 4000 Fruit Ninja(树状数组)
2017-08-06 22:22
330 查看
Fruit Ninja
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2529 Accepted Submission(s): 988
Problem Description
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 ?
Input
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.
Output
For each test case, ouput the number of triples as the sample
4000
below, you just need to output the result mod 100000007.
Sample Input
2
6
1 3 2 6 5 4
5
3 5 2 4 1
Sample Output
Case #1: 10
Case #2: 1
Source
2011 Multi-University Training Contest 16 - Host
by TJU
题意:
给你一个数列,求 x < z < y 的组合。
POINT:
可以分2次考虑:
1.从前到后求,先固定x,找出前面有几个比x大的,就能直接算出后面有几个比x大的。那么x < z < y和x < y <
z的组合数量就确定下来了。
2.去除x
< y < z,固定y,找出前面有几个比y小的,找出后面有几个比y大的。那么x < y < z组合数就确定下来了。
具体可以看看TLE的代码2。
AC代码1:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; #define LL long long const int N = 100000; const int p = 100000007; int sum[N+3]; int lowbit(int x) { return x&-x; } void add(int x,int y) { for(int i=x;i<=N;i+=lowbit(i)) { sum[i]+=y; } } int query(int x) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)) { ans+=sum[i]; } return ans; } int a ; int main() { int T; scanf("%d",&T); int o=0; while(T--) { int n; scanf("%d",&n); memset(sum,0,sizeof sum); LL ans=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); add(a[i],1); LL bigl=query(N)-query(a[i]); LL saml=query(a[i]-1); LL bigr=n-a[i]-bigl; ans+=bigr*(bigr-1)/2; ans-=saml*bigr; ans=(ans+p)%p; } printf("Case #%d: ",++o); printf("%lld\n",ans); } }
代码2:TLE,而且没有LL会wa,所以容易想到把两个操作和起来就行了。
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; #define LL long long const int N = 100000; const int p = 100000007; int sum[N+3]; int lowbit(int x) { return x&-x; } void add(int x,int y) { for(int i=x;i<=N;i+=lowbit(i)) { sum[i]+=y; } } int query(int x) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)) { ans+=sum[i]; } return ans; } int a ; int bigr ; int main() { int T; scanf("%d",&T); int o=0; while(T--) { int n; scanf("%d",&n); memset(sum,0,sizeof sum); int ans=0; for(int i=1;i<=n;i++)//步骤1 { scanf("%d",&a[i]); add(a[i],1); int bigl=query(N)-query(a[i]); bigr[i]=n-a[i]-bigl; ans+=bigr[i]*(bigr[i]-1)/2; ans%=p; } memset(sum,0,sizeof sum); for(int i=1;i<=n;i++)//步骤2 { add(a[i],1); int saml=query(a[i]-1); ans-=saml*bigr[i]; ans=(ans+p)%p; } printf("Case #%d: ",++o); printf("%d\n",ans); } }
相关文章推荐
- HDU 4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja[树状数组]
- hdu4000 Fruit Ninja(树状数组)
- hdu 4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja 【树状数组】
- HDU 4000 Fruit Ninja 树状数组 + 计数
- hdu 4000 Fruit Ninja(树状数组)
- HDU - 4000 - Fruit Ninja (树状数组)
- HDU 4000 Fruit Ninja (树状数组+组合问题)
- hdu 4000 - Fruit Ninja(树状数组)
- hdu4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja (树状数组+反向思维)
- hdu 4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja(树状数组)
- HDOJ 题目4000 Fruit Ninja(树状数组)
- hdu4000-树状数组-2
- 【HDU 4000】 树状数组
- hdoj4000Fruit Ninja【树状数组】
- HDU 4000 Fruit Ninja && HLG 1625 ikki的数字 (树状数组)
- hdoj 4000 Fruit Ninja 【树状数组 + 思维】