Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3642 Accepted Submission(s): 1113 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
Recommend lcy
|
题目大意:给你一个N个数(1-N)的一个排列,求所有的i<j<k且a[i]<a[k]<a[j]的个数,也就是<小,大,中>这样的排列有多少个,
思路:这个题目要求的是<小,大,中>的这样的排列,我们可以先加上一个<小,中,大>的排列然后在减去,
最后发现是不是结果:ans=<小,大,中> + < 小,中,大> - <小,中,大>
==》<小,比小大,比小大> - <小,中,大>;
弄成最后这样子就比较好算了,前面的无非就是个C(n,2),其中n是指比他大的数从中挑出两个来就可以,用树状数组,相当于是用木桶排序了,直接可以知道在他前面的有多少个数,算一下就好了
最后减去的就是顺次递增的。。。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int a, n, c[100001];
long long ans, tmp, tmp1;
int lowbit(int x)
{
return x&(-x);
}
void update(int i)
{
while(i<=n)
{
c[i]+=1;
i+=lowbit(i);
}
}
int sum(int i)
{
int s=0;
while(i>0)
{
s += c[i];
i-=lowbit(i);
}
return s;
}
int main()
{
int i, T, cnt=1;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
ans = 0;
memset(c,0,sizeof(c));
for(i=1; i<=n; i++)
{
scanf("%d", &a);
update(a);
tmp = sum(a-1);
tmp1 = (n-a)-(i-1-tmp);
ans -= tmp*tmp1;
if(tmp1>=2)
ans += tmp1*(tmp1-1)/2;
}
ans %=100000007;
printf("Case #%d: %I64d\n", cnt++, ans);
}
return 0;
}