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

HDU 4000 Fruit Ninja(树状数组)

2017-04-20 15:47 435 查看

Fruit Ninja

[align=left]Problem Description[/align]
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 ?

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each test case, ouput the number of triples as the sample below, you just need to output the result mod 100000007.

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

Case #1: 10
Case #2: 1

看了网上的思路才解出,这题做的真的很不愉快!,各种WA,要注意long long 类型,和求余

题意:给一个序列,1~N,找三个数,x,y,z(下标i<j<k),满足(x<z<y),这类三元组的个数

分析:树状数组求解,首先找到(x<y<z)的个数a,然后求(x<y?z)的个数b,b-a的个数,即是要求得个数;

1、对每一个数,求得前面小于它的个数t1,后面大于它的个数t2,即 a=t1*t2;

2、从x开始,从后面大于它的数中任选两个,求得个数(b=(s-1)*s/2)。

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
#define Mod 100000007
const int maxn=1e5+10;
typedef long long LL;
int c[maxn];
int N;

int lowbit(int i){
return i&-i;
}

int sum(int i){
int cnt=0;
while(i){
cnt+=c[i];
i-=lowbit(i);
}
return cnt;
}

void update(int i){
while(i<=N){
c[i]++;
i+=lowbit(i);
}
}

int main(){
int T;
scanf("%d",&T);
int count =0;
while(T--){
scanf("%d",&N);
memset(c,0,sizeof(c));
int a;
LL Min,Max;//这里要设置为LL型,极限数字10w*10w结果溢出整型
LL tmp=0;
for(int i=1;i<=N;i++){
scanf("%d",&a);
update(a);
Min=sum(a-1);
Max=(N-a)-(i-1-Min);
tmp+=Max*(Max-1)/2-Min*Max;
}
printf("Case #%d: %lld\n",++count,tmp%Mod); //Mod只能放在这里求余,原因是多种运算符混杂,运算过程中求余,运算结果不正确
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: