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
55
56
57
58
59
60
#include <iostream>
#include <math.h>
#include <vector>

typedef long long int ll;

int N;
ll POW = 1;

ll ARMY[44], COLL[44], BEST[44], AMOUNT[44];

std::vector<int> BITES;
std::vector<int> SCORE;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin >> N;

    for (int i = 0; i < N; i ++) {
        std::cin >> ARMY[i];
        POW *= 2;
        BEST[i+1] = N * N * N;
    }
    BITES = std::vector<int>(POW, 0);
    SCORE = std::vector<int>(POW, 0);

    for (int i = 1; i < POW; i ++)
        BITES[i] = BITES[i / 2] + i % 2;

    for (int i = 0; i < N; i ++) {
        for (int j = N - 1; j >= 0; j--) {
            COLL[i] *= 2;
            if (i > j && ARMY[i] < ARMY[j])
                COLL[i] += 1;
            else if (i < j && ARMY[i] > ARMY[j])
                COLL[i] += 1;
        }
    }

    ll CURRENT = 0, MASK = 1;
    for (int i = 1; i < POW; i ++) {
        if (i == MASK * 2) {
            CURRENT += 1;
            MASK *= 2;
        }

        int collisions = BITES[COLL[CURRENT] & i]; //nowe kolizje
        SCORE[i] = SCORE[i - MASK] + collisions;

        if (SCORE[i] == BEST[BITES[i]]) {
            AMOUNT[BITES[i]] += 1;
        } else if (SCORE[i] < BEST[BITES[i]]) {
            BEST[BITES[i]] = SCORE[i];
            AMOUNT[BITES[i]] = 1;
        }
    }

    for(int i = 1; i <= N; i++)
        std ::cout << BEST[i] << " " << AMOUNT[i] << std::endl;
}