hdu 4000 Fruit Ninja 求数组中 小大中 的个数
2011-09-08 12:51
337 查看
[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]
[align=left]Sample Output[/align]
//小中大+小大中=sigma (rightmax-1)*rightmax/2=total
//小中大 = sigma leftmin*rightmax
//小大中=total-小中大
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110000;
const int mod=100000007;
int c[maxn];
int n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
int sum(int x)
{
int cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
long long leftmin[maxn],rightmax[maxn];
int main()
{
int ci,pl=1;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
long long ans=0;
for(int i=1;i<=n;i++)
{
leftmin[i]=sum(a[i]);
update(a[i],1);
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
rightmax[i]=sum(n)-sum(a[i]);
update(a[i],1);
}
for(int i=1;i<=n;i++)
{
long long tmp=((rightmax[i]-1)*rightmax[i]/2)%mod;
long long cnt=(leftmin[i]*rightmax[i])%mod;
long long tnow=((tmp-cnt)%mod+mod)%mod;
ans=(ans+tnow)%mod;
}
printf("Case #%d: %I64d\n",pl++,ans);
}
return 0;
}
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
//小中大+小大中=sigma (rightmax-1)*rightmax/2=total
//小中大 = sigma leftmin*rightmax
//小大中=total-小中大
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110000;
const int mod=100000007;
int c[maxn];
int n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
int sum(int x)
{
int cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
long long leftmin[maxn],rightmax[maxn];
int main()
{
int ci,pl=1;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
long long ans=0;
for(int i=1;i<=n;i++)
{
leftmin[i]=sum(a[i]);
update(a[i],1);
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
rightmax[i]=sum(n)-sum(a[i]);
update(a[i],1);
}
for(int i=1;i<=n;i++)
{
long long tmp=((rightmax[i]-1)*rightmax[i]/2)%mod;
long long cnt=(leftmin[i]*rightmax[i])%mod;
long long tnow=((tmp-cnt)%mod+mod)%mod;
ans=(ans+tnow)%mod;
}
printf("Case #%d: %I64d\n",pl++,ans);
}
return 0;
}
相关文章推荐
- hdu 4000 - Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja[树状数组]
- hdu4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja(树状数组)
- hdu 4000 Fruit Ninja(树状数组)
- HDU - 4000 - Fruit Ninja (树状数组)
- HDU 4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja (树状数组+反向思维)
- HDU 4000 Fruit Ninja 树状数组 + 计数
- HDU 4000 Fruit Ninja 【树状数组】
- hdu 4000 Fruit Ninja(树状数组)
- hdu 4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja (树状数组+组合问题)
- hdu4000 Fruit Ninja(树状数组)
- HDU 4000 Fruit Ninja(树状数组)
- hdu 4000 (Fruit Ninja )(树状数组+数学知识)
- hdu 4000Fruit Ninja 树状数组
- hdoj4000Fruit Ninja【树状数组】
- hdu 4000 Fruit Ninja 树状数组
- 【HDU 4000】 树状数组