Fruit Ninja(树状数组+思维)

时间:2023-12-30 13:16:44

Fruit Ninja

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

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
题解:
给你一个1到n的排列,让求满足posx < posy < posz  && x < z < y 的组数有多少。
宇神写的非常好,思路是对于每个数找出后面比a[i]大的数n2;则可以组成的i < j < k && (a[i] < a[j] < a[k] || a[i] < a[k] < a[j])的组合数为C(2,n2);再减去i < j < k && a[i] < a[j] < a[k]就好了,其实就是前面比a[i]小的数乘以后面比a[i]大的数;另外,要用LL
代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MAXN=1e5+;
const int MOD=;
LL tree[MAXN+];
int lowbit(int x){return x&(-x);}
void update(int x,int y){
while(x<=MAXN){
tree[x]++;
x+=lowbit(x);
}
}
LL sum(int x){
LL sum=;
while(x>){
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main(){
int T,N,temp,flot=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
mem(tree,);
LL n1,n2;
LL ans=;
for(int i=;i<=N;i++){
scanf("%d",&temp);
update(temp,);
n1=sum(temp-);//比temp小的;
n2=N-temp-(i-n1-);
// printf("%d %d\n",n1,n2);
ans+=n2*(n2-)/;
ans-=n1*n2;
}
printf("Case #%d: %lld\n",++flot,ans%MOD);
}
return ;
}