hdoj 4000 Fruit Ninja 【树状数组 + 思维】



Fruit Ninja

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


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
 



题意:给定n个元素的序列,问你满足i < j < k 且 a[i] < a[k] < a[j]的(i, j, k)组合有多少个。


思路:可以先求解i < j < k && (a[i] < a[j] < a[k] || a[i] < a[k] < a[j])的组合数。找到在a[i]后面有多少个数比它大就可以了(设为num2),方案数为num2*(num2-1) / 2,然后去掉i < j < k && a[i] < a[j] < a[k]的情况(前面比a[i]小的数的个数 * num2)就是答案了。



AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 100000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
LL C[MAXN];
int lowbit(int x){
    return x & (-x);
}
int n;
void update(int x, int d)
{
    while(x <= n)
    {
        C[x] += d;
        x += lowbit(x);
    }
}
LL sum(int x)
{
    LL s = 0;
    while(x > 0)
    {
        s += C[x];
        x -= lowbit(x);
    }
    return s;
}
int main()
{
    int t, kcase = 1; Ri(t);
    W(t)
    {
        Ri(n);
        CLR(C, 0);
        LL ans = 0;
        for(int i = 1; i <= n; i++)
        {
            int a; Ri(a);
            update(a, 1);
            LL num1 = sum(a-1);
            LL num2 = n-a - (i-num1-1);
            if(num2 >= 2)
                ans += num2 * (num2 - 1) / 2;
            ans -= num1 * num2;
        }
        ans %= MOD;
        printf("Case #%d: %lld\n", kcase++, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值