#include <bits/stdc++.h> typedef long long int lli; using namespace std; #define MAX3 1010 #define MAX4 10010 #define MAX5 100010 #define MAX6 1000010 template <typename T > void dbg(T last) { cout << last << "\n"; } template <typename T, typename ...args> void dbg(T current, args ...next) { cout << current << ' '; dbg(next...); } vector<vector<pair<int,int>>> edges_; int distances[MAX6]; class Djikstra { // std::vector<std::vector<std::pair<int,int>>> *edges_; int start_; int maxNode_; public: Djikstra(int start, int maxNode) : start_(start), maxNode_(maxNode) {} // std::vector<int> parents_; void get() { std::priority_queue<pair<int,int>, std::vector<std::pair<int,int>>, std::greater<std::pair<int,int>>> pq_; // distances.resize(maxNode_ + 1, INT_MAX); distances[start_] = 0; pq_.push(std::make_pair(0,start_)); // parents_.resize(maxNode_ + 1, 0); // parents_[start_] = start_; while(!pq_.empty()) { auto up = pq_.top(); pq_.pop(); int u = up.second; // dbg(u); for (auto vp : edges_[u]) { if (distances[u] + vp.first < distances[vp.second]) { // parents_[vp.second] = u; distances[vp.second] = distances[u] + vp.first; pq_.push(std::make_pair(distances[vp.second], vp.second)); } } } } }; int n; int tab[MAX6]; int ans[MAX6]; bool vis[MAX6]; void calc(int x) { for (int i=1;i<=n;i++) { distances[i] = INT_MAX; } Djikstra d(x, n); d.get(); for (int i=1;i<=n;i++) { int y = distances[i]; if (y != INT_MAX) { ans[x] += y; } } } void solve() { cin >> n; edges_.resize(n+2); for (int i=1;i<=n;i++) { cin >> tab[i]; } for (int i=1;i<=n;i++) { int x1 = i; int y1 = tab[i]; for (int j=i+1;j<=n;j++) { int x2 =j; int y2 = tab[j]; // dbg(x1, y1, x2, y2); if ((x1 < x2 && y2 <y1) || (x2 < x1 && y1 <y2) ) { edges_[i].push_back(make_pair(1,j)); edges_[j].push_back(make_pair(1,i)); } } } for (int i=1;i<=n;i++) { calc(i); cout << ans[i] << " "; } cout << "\n"; } int main() { std::ios::sync_with_stdio(false); solve(); return 0; } /* 7 2 1 4 7 3 6 5 */
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <bits/stdc++.h> typedef long long int lli; using namespace std; #define MAX3 1010 #define MAX4 10010 #define MAX5 100010 #define MAX6 1000010 template <typename T > void dbg(T last) { cout << last << "\n"; } template <typename T, typename ...args> void dbg(T current, args ...next) { cout << current << ' '; dbg(next...); } vector<vector<pair<int,int>>> edges_; int distances[MAX6]; class Djikstra { // std::vector<std::vector<std::pair<int,int>>> *edges_; int start_; int maxNode_; public: Djikstra(int start, int maxNode) : start_(start), maxNode_(maxNode) {} // std::vector<int> parents_; void get() { std::priority_queue<pair<int,int>, std::vector<std::pair<int,int>>, std::greater<std::pair<int,int>>> pq_; // distances.resize(maxNode_ + 1, INT_MAX); distances[start_] = 0; pq_.push(std::make_pair(0,start_)); // parents_.resize(maxNode_ + 1, 0); // parents_[start_] = start_; while(!pq_.empty()) { auto up = pq_.top(); pq_.pop(); int u = up.second; // dbg(u); for (auto vp : edges_[u]) { if (distances[u] + vp.first < distances[vp.second]) { // parents_[vp.second] = u; distances[vp.second] = distances[u] + vp.first; pq_.push(std::make_pair(distances[vp.second], vp.second)); } } } } }; int n; int tab[MAX6]; int ans[MAX6]; bool vis[MAX6]; void calc(int x) { for (int i=1;i<=n;i++) { distances[i] = INT_MAX; } Djikstra d(x, n); d.get(); for (int i=1;i<=n;i++) { int y = distances[i]; if (y != INT_MAX) { ans[x] += y; } } } void solve() { cin >> n; edges_.resize(n+2); for (int i=1;i<=n;i++) { cin >> tab[i]; } for (int i=1;i<=n;i++) { int x1 = i; int y1 = tab[i]; for (int j=i+1;j<=n;j++) { int x2 =j; int y2 = tab[j]; // dbg(x1, y1, x2, y2); if ((x1 < x2 && y2 <y1) || (x2 < x1 && y1 <y2) ) { edges_[i].push_back(make_pair(1,j)); edges_[j].push_back(make_pair(1,i)); } } } for (int i=1;i<=n;i++) { calc(i); cout << ans[i] << " "; } cout << "\n"; } int main() { std::ios::sync_with_stdio(false); solve(); return 0; } /* 7 2 1 4 7 3 6 5 */ |