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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#include <cstdio>
#include <cinttypes>
#include <vector>
#include <map>
#include <queue>

using namespace std;

struct Node
{
    int idx;
    int length;
    Node(int i, int l) : idx(i), length(l) { }
};

bool operator < (const Node& n1, const Node& n2)
{
    return n1.length > n2.length;
}

int main()
{
    int n, k;
    vector<int> line;
    vector<vector<int>> edges;
    vector<map<int, int>> shortestPaths;

    scanf("%i", &n);
    line.resize(n+1);
    edges.resize(n+1);
    shortestPaths.resize(n+1);

    for(int i = 1; i <= n; i++)
    {
        scanf("%i", &k);
        line[i] = k;
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = i+1; j <= n; j++)
        {
            if(line[i]>line[j])
            {
                edges[i].push_back(j);
                edges[j].push_back(i);
            }
        }
    }

    for(int i = 1; i <= n; i++)
    {
        priority_queue<Node> queue;
        vector<bool> visited(n+1, false);

        queue.push(Node(i, 0));

        while(!queue.empty())
        {
            Node node = queue.top();
            queue.pop();

            if(visited[node.idx])
            {
                continue;
            }
            visited[node.idx] = true;

            shortestPaths[i][node.idx] = node.length;

            for(int nextNode : edges[node.idx])
            {
                if(!visited[nextNode])
                {
                    queue.push(Node(nextNode, node.length+1));
                }
            }
        }
    }

    for(int i = 1; i <= n; i++)
    {
        int64_t sum = 0;

        for(auto& item : shortestPaths[i])
        {
            sum+= item.second;
        }

        if (i == 1)
        {
            printf("%lld", sum);
        }
        else
        {
            printf(" %lld", sum);
        }
    }

    printf("\n");
}