Number of Ikki |
Time limit:1000 MS |
Memory limit:32768 K |
Total submit:22(users) |
Total Accepted:9(8 users) |
Rating: |
Special Judge: No |
|
Description |
Ikki recently became interested in numbers. Now Ikki write a continuous N number on the paper, each number is a random number between [1,n] and do not repeat, that is, This string of numbers is a permutation of the digital 1~n. Number of numbers from 1 to N, now Ikki want to test you: In this n number can find out how many 3 number of combinations satisfy: num[x]<num[z]<num[y] and X<y<z, in which x, Y, Z is the subscript of these three numbers.
|
Input |
Multiple sets of test data, the first line an integer t represents the number of test data.
For each set of data, the first line enters an integer N representing the number of digits in the series (1<=n<=5000) The second line enters n numbers to indicate the arrangement of a 1~n.
|
Output |
For each set of data, the output "case #k: P", K denotes the K-group example, p represents the number of combinations of 3 numbers that satisfy the requirement, and one row for each set of outputs. Because the results may be larger, the result is 100000007 modulo. |
Sample Input |
2 6 1 3 2 6 5 4 5 3 5 2) 4 1
|
Sample Output |
Case #1:10 Case #2:1
|
Author |
Eric @hrbust
Hidden Tree Array ~ ~ ~ did not see, in fact, the main is no idea, thinking out of the ability to use a tree-like array to solve Infer the total number of groups satisfying i<j<k and num[i]<num[k]<num[j] use a tree array to find the number of numbers in front of a number that is smaller than it, and then be able to know the number of previous numbers that are larger than it. The total number of larger than it minus the number before it is equal to the number of larger than it, Cn2 = x* (x-1)/2;then be sure to subtract the total number of i<j<k and A[i]<a[j]<a[k] from the back;
- the total number of (xyz,xzy) can be calculated first
- Just need to go out the number of n,c (n,2) After X is the
-
- for A, the number of smaller than a low[a], than a large number of high[a],low[a]*high[a] is the answer
- can use tree-like array to find the above number
Attention, the overall solution!Not to consider a number alone. #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;}
|
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
hdu4000 && hrbust1625