Magic Pairs

Hello, I am trying to solve this problem

Alexandra has some distinct integer numbers a1,a2...an.
Count number of pairs (i,j) such that:
1≤ i ≤ n
1≤ j ≤ n
ai < aj

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.The first line of each test case contains a single integer n denoting the number of numbers Alexandra has. The second line contains n space-separated distinct integers a1, a2, ..., an denoting these numbers.

Output

For each test case, output a single line containing number of pairs for corresponding test case.

Constraints

1 ≤ T ≤ 4
1 ≤ n ≤ 100000
0 ≤ ai ≤ 109
All the ai are distinct

Example

2
2
2 1
3
3 1 2
Output:
1
3

Explanation

Case 1: Only one such pair: (2,1)
Case 2: 3 possible pairs: (2,1), (2,3), (3,1)

as I understand the problem is just counting how many Ai's are 1 <= Ai <= N and then apply ((R*(R-1))/2), R is the count of valid Ai's

My actual code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <stdio.h>

    using namespace std;

    #ifndef ONLINE_JUDGE
        #define getNumber getchar()
        #define getString getchar()
    #else
        #define getNumber getchar_unlocked()
        #define getString getchar_unlocked()
    #endif

    typedef long long int ll_t;

    inline ll_t ReadFastInt()
    {
        ll_t num = 0;
        register char charNumber;

        while(1)
        {
            charNumber  = getNumber;
            if((charNumber < '0') || (charNumber > '9'))
                break;

            num = (num << 3) + (num << 1) + charNumber - '0';
        }
        return num;
    }

    int main()
    {
        ll_t T = ReadFastInt();

        while (T--)
        {
            ll_t N = ReadFastInt();
            ll_t temp = N, R = 0, A = 0;
            while(N--)
            {
                A = ReadFastInt();
                if((A<=temp)&&(A>=1))
                    R++;
            }

            R = ((R*(R-1))/2);

            printf("%lld\n", R);
        }
        return 0;
    }



but I still get wrong answer with the online judge, I don't know what is the problem

Could someone help me understand the question so I can fix my solution?
> 1 ≤ n ≤ 100000
> 0 ≤ ai ≤ 109
> All the ai are distinct
¿are you sure? in that case `n' would be restricted to [1; 110] (pigeonhole principle)

> just counting how many Ai's are 1 <= Ai <= N
¿why?
If you input was: 42 54
¿wouldn't the pair {42,54} be valid?


> ReadFastInt()
yeah, sure
Last edited on
If N is for example 10 any Ai = 0 or Ai > 10 is ignored so if the input is

1 2 0 15 4 6 80 16 9 7

only 1 2 4 6 9 7 are valid Ai's for my understanding if I just count the "valid Ai's" and apply the formula for R it should be enough

The reason I am counting only how many Ai's are 1 <= Ai <= N is because of the restriction

1≤ i ≤ N
1≤ j ≤ N

given in the problem statement

What do you mean with

"> ReadFastInt()
yeah, sure" ?

I don't know a fastest way, but for sure this is fastest than cin and scanf

> The reason I am counting only how many Ai's are 1 <= Ai <= N is because of the restriction
> 1≤ i ≤ N
> 1≤ j ≤ N
That restriction is to the index, no to the elements. Is just to say that a_i and a_j can be any element from the set
So for the example I gave

If N is for example 10 any Ai = 0 or Ai > 10 is ignored so if the input is

1 2 0 15 4 6 80 16 9 7

then if I apply the formula ((R*(R-1))/2) it should work, so for N=10 then R is 45 right?

I already tried that and still getting wrong answer
Topic archived. No new replies allowed.