Hdu4000 & hrbust1625

Source: Internet
Author: User
Ikki numbers
Time Limit: 1000 MS Memory limit: 32768 K
Total submit: 22 (12 Users) Total accepted: 9 (8 Users) Rating: Special Judge: No
Description

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.

Input
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.


Output

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.

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

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;

  1. You can first find the total number of (xyz, xzy)
  2. You only need to get out of the number of N, C (n, 2) after X.
  3. Then obtain the number of XYZ,
  4. For a, calculate the number lower than a [a], the number higher than a [a], and low [a] * High [a] is the answer.
  5. 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;}

Hdu4000 & hrbust1625

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.