Ikki is interested in numbers recently. Now Ikki writes n consecutive numbers on the paper. Each number is an arbitrary number between [1, N] and is not repeated. Is the number 1 ~ An array of numbers ranging from 1 to n. Now Ikki wants to test you: The combination of the N numbers can be found to meet the following requirements: num [x] <num [Z] <num [y] and x <Y <z, where X, Y and Z are the subscript of the three numbers.
|
Multiple groups of test data, the first line of an integer t represents the number of test data.
For each group of data, enter an integer N in the first row to indicate the number of numbers in the series (1 <= n <= 5000) Input n numbers in the second line to indicate a 1 ~ N.
|
For each group of data, "Case # K: P" is output, and "K" indicates the k-th Group of samples. "P" indicates the number of combinations of the three numbers that meet the requirements. Each group of outputs occupies one row. Because the result may be relatively large, the result needs to be modulo 100000007. |
Zhou @ hrbust
Hiding tree Arrays ~~~ I didn't see it at all. In fact, I don't have any idea. I can solve it with a tree array when I come up with the idea. Determine the total number of groups that meet the requirements of I <j <K and num [I] <num [k] <num [J]. A tree array can be used to obtain a number smaller than the former., then we can know the number of numbers greater than it, the total number greater than it minus the number greater than it is equal to the number greater than it, cn2 = x * (x-1) /2; then, you must subtract the number of all the components I <j <K and A [I] <A [J] <A [K;
- You can first find the total number of (xyz, xzy)
- You only need to get out of the number of N, C (n, 2) after X.
- Then obtain the number of XYZ,
- For a, calculate the number lower than a [a], the number higher than a [a], and low [a] * High [a] is the answer.
- You can use a tree array to calculate the preceding number.
Pay attention to the problem! This does not mean that a certain number is considered separately.#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int mod = 100000007;int T,n;int c[5010];int lowbit(int x){ return x & -x;}LL getsum(int x){ LL sum = 0; while(x > 0) { sum += c[x]; x -= lowbit(x); } return sum;}void update(int x , int val){ while(x <= n) { c[x] += val; x += lowbit(x); }}int main(){#ifdef xxz freopen("in.txt","r",stdin);#endif // xxz cin>>T; int Case = 1; while(T--) { memset(c,0,sizeof(c)); cin>>n; LL ans = 0; for(int i = 1; i <= n; i++) { int temp; cin>>temp; update(temp,1); LL presmaller = getsum(temp - 1); LL prebigger = i-1 - presmaller; LL totbigger = n - temp; LL afterbigger = totbigger - prebigger; ans -= presmaller * afterbigger; if(afterbigger >= 2) { ans += afterbigger*(afterbigger - 1)/2; } } cout<<"Case #"<<Case++<<": "<<ans%mod<<endl; } return 0;}
|