HDU4000 Fruit Ninja利用树状数组计数+组合数学

http://acm.hdu.edu.cn/showproblem.php?pid=4000

让找i<j<k,但是ai<ak<aj的数

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

 

2 6 1 3 2 6 5 4 5 3 5 2 4 1

 

 

Sample Output

 

Case #1: 10 Case #2: 1

根据树状数组的存储特点,从小到大存贮;
用树状数组存储的话就可以很快找到它左边比她小的数;
就能推理出右边比他大的数;
从右边比他大的数中挑选两个,然后推理出它的下一位,可以计算出它的下一位当做中间数得到的小中大顺序数
然后用sum1-sum2即可
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<vector>
const int maxn=5e5+5;
const int mod=1e8+7;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int n,bit[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x)
{
    while(x<=n)
    {
        bit[x]++;//如果这个函数另一种形式还有个参数,就是bit[x]+=v;
        x+=lowbit(x);
    }
}
int getsum(int x)
{
    int s=0;
    while(x)
    {
        s+=bit[x];
        x-=lowbit(x);
    }
    return s;
}
int main()
{
    int t,Case=0;
    cin>>t;
    while(t--)
    {
        me(bit,0);
        scanf("%d",&n);
        ll sum=0,x,l,r;//l,前面输入比x小的数的个数,r表示输入数中比x大的数的个数。
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&x);
            updata(x);
            //if(i==0||i==n-1)continue;
            l=getsum(x-1);//看它左边比她小的;
            r=(n-x)-(i-l-1);//(n-x)表示有这么多个数比x大,(i-1-l)表示前i-1个数里面有sum(x-1)个数比a大
            sum+=r*(r-1)/2-r*l;//这个是减去以第二个数为中间数,因为总要减去,最终减去他们的和就可以
        }
        printf("Case #%d: %lld\n",++Case,sum%mod);
    }
    return 0;
}
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值