hdoj4000Fruit Ninja【树状数组】

来源:互联网 发布:拷贝文件 linux 编辑:程序博客网 时间:2024/05/17 06:02

Fruit Ninja

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


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

题意: 给出一列1-n,n个数求满足对下标 i,j,k满足i<j<k且num[i]<num[k]<num[j]的三元组数量

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#define MOD 100000007using namespace std;const int maxn=100010;int c[maxn];int low(int x){    return x&(-x);}int sum(int n){    int value=0;    while(n>0){        value+=c[n];        n-=low(n);    }    return value;}void add(int pos,int n){    while(pos<=n){        c[pos]+=1;        pos+=low(pos);    }}int main(){    int n,i,j,k=1,t,num;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        memset(c,0,sizeof(c));        long long ans=0;        for(i=1;i<=n;++i){            scanf("%d",&num);            long long num1=sum(num);//num前比num小的数             long long num2=(n-num)-(i-1-num1);//比num大的数             ans=ans+num2*(num2-1)/2;//从num后面大于num数中任选两个             ans=ans-num1*num2;//满足i<j<k,且num[i]<[j]<[k] 的数的数量             ans=(ans+MOD)%MOD;            add(num,n);        }        printf("Case #%d: %lld\n",k++,ans);    }    return 0;}


0 0
原创粉丝点击