您的位置:首页 > 产品设计 > UI/UE

HDU - 4000 - Fruit Ninja (树状数组)

2017-08-15 14:24 441 查看


Fruit Ninja

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


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

 

Source

2011 Multi-University Training
Contest 16 - Host by TJU 

 

题意:在一个1-n的排列有多少个(x,y,z)满足x<y<z且Ax<Az<Ay

思路:
对每一个数进行处理,使每一个数为x,其后面有m个比A[x]大的数,可以得到 Ax<Ay<Az、Ax<Az<Ay (x<y<z),两种共 m*(m-1)/2个(从m个中取两个,组合数),然后要减去其中 Ax<Ay<Az 的个数就是该点作为x得到的符合题目要求的数量。
求Ax<Ay<Az 的个数可以把每个数看作y,求出y左边比Ay小的个数k和y右边比Ay大的个数b,得到以该点为y得到的Ax<Ay<Az 的个数为k*b。
题目要求要取模,然后因为不是一直增加,所以要注意一下。

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define LL long long
int n,cas = 0,a[101000],t,num;
LL ans,mod = 100000007;
int lowbit(int x)
{
return x&-x;
}
void add(int pos)
{
while(pos<=n)
{
a[pos]++;
pos+=lowbit(pos);
}
}
int sum(int pos)
{
int ans = 0;
while(pos>0)
{
ans+=a[pos];
pos-=lowbit(pos);
}
return ans;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof a);
ans = 0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&num);
add(num);
LL k = sum(num-1);//在该点前比num小的数的个数
LL m = i - k;//在该点前比num大的数的个数
LL b = n - num - m;//在该点后比num大的数的个数
if(b>=2)
ans += b * (b-1) / 2;
ans -= k * b;
ans = (ans+mod)%mod;
}
printf("Case #%d: %lld\n",++cas,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: