#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <chrono>
#include <functional>
#include <cstdlib>
#include <cassert>
#include <sstream>
#include <climits>
using namespace std;
struct ANode
{
std::vector<int> neighbours;
};
int main()
{
//ios::sync_with_stdio(false);
//std::cin.ignore(256, '\n');
const clock_t begin_time = clock();
int n;
int k;
std::cin >> n;
std::vector<int> perm;
for (int i = 0; i < n; ++i)
{
int k;
std::cin >> k;
perm.push_back(k-1);
}
std::vector<ANode> nodes;
for (int i = 0; i < n; ++i)
{
ANode node;
for (int j = 0; j < n; ++j)
{
if (j == i)
continue;
if (i<j && perm[i] > perm[j])
node.neighbours.push_back(j);
if (i>j && perm[i] < perm[j])
node.neighbours.push_back(j);
}
nodes.push_back(node);
}
for (int i = 0; i < n; ++i)
{
std::vector<bool> reached(n, false);
int count = 0;
int height = 0;
std::vector<int> current;
current.push_back(i);
reached[i] = true;
while (!current.empty())
{
height++;
std::vector<int> newCurrent;
for (int j = 0; j < current.size(); ++j)
{
for (int k = 0; k < nodes[current[j]].neighbours.size(); ++k)
{
int neighbour = nodes[current[j]].neighbours[k];
if (reached[neighbour] == false)
{
newCurrent.push_back(neighbour);
reached[neighbour] = true;
}
}
}
count += height * newCurrent.size();
current = newCurrent;
}
std::cout << count << " ";
}
//std::vector<std::vector<long long>>
//testMaxTest();
//testBigNumbers();
//testFractions();
//std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n";
return 0;
};
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 | #include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <chrono> #include <functional> #include <cstdlib> #include <cassert> #include <sstream> #include <climits> using namespace std; struct ANode { std::vector<int> neighbours; }; int main() { //ios::sync_with_stdio(false); //std::cin.ignore(256, '\n'); const clock_t begin_time = clock(); int n; int k; std::cin >> n; std::vector<int> perm; for (int i = 0; i < n; ++i) { int k; std::cin >> k; perm.push_back(k-1); } std::vector<ANode> nodes; for (int i = 0; i < n; ++i) { ANode node; for (int j = 0; j < n; ++j) { if (j == i) continue; if (i<j && perm[i] > perm[j]) node.neighbours.push_back(j); if (i>j && perm[i] < perm[j]) node.neighbours.push_back(j); } nodes.push_back(node); } for (int i = 0; i < n; ++i) { std::vector<bool> reached(n, false); int count = 0; int height = 0; std::vector<int> current; current.push_back(i); reached[i] = true; while (!current.empty()) { height++; std::vector<int> newCurrent; for (int j = 0; j < current.size(); ++j) { for (int k = 0; k < nodes[current[j]].neighbours.size(); ++k) { int neighbour = nodes[current[j]].neighbours[k]; if (reached[neighbour] == false) { newCurrent.push_back(neighbour); reached[neighbour] = true; } } } count += height * newCurrent.size(); current = newCurrent; } std::cout << count << " "; } //std::vector<std::vector<long long>> //testMaxTest(); //testBigNumbers(); //testFractions(); //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n"; return 0; }; |
English