HDOJ 题目4000 Fruit Ninja(树状数组)

来源:互联网 发布:mac mini nas 方案 编辑:程序博客网 时间:2024/06/05 13:35

Fruit Ninja

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2137    Accepted Submission(s): 830


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 below, you just need to output the result mod 100000007.
 

Sample Input
261 3 2 6 5 45 3 5 2 4 1
 

Sample Output
Case #1: 10Case #2: 1
 

Source
2011 Multi-University Training Contest 16 - Host by TJU
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  4004 4002 4001 3998 3996 
 题目大意:就是让从这个序列找三个数x,y,z.使x<z<y,问有多少种选法
152558132015-10-26 11:46:03Accepted4000483MS2476K958 BC++Who_you?
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define ll long long#define mod  100000007using namespace std;ll a[100005],n;ll lowbit(ll x){    return x&(-x);}void add(ll x){    while(x<=n)    {        a[x]+=1;        x+=lowbit(x);    }}ll getsum(ll x){    ll num=0;    while(x>0)    {        num+=a[x];        x-=lowbit(x);    }    return num;}int main(){    int t,c=0;    scanf("%d",&t);    while(t--)    {        //int n;        scanf("%d",&n);        int i;        memset(a,0,sizeof(a));        ll ans=0;        for(i=1;i<=n;i++)        {            ll x;            scanf("%lld",&x);            add(x);            ll tmp1=getsum(x-1);//前边比x小的数            ll tmp2=n-x-(i-tmp1-1);//后边比x大的数            ans-=tmp1*tmp2;//去掉X<Y<Z的情况            if(tmp2>=2)                ans+=tmp2*(tmp2-1)/2;//从x后边随便取两个比它大的数        }        printf("Case #%d: %lld\n",++c,ans%mod);    }}


0 0
原创粉丝点击