#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int t[200005], odl[200005];
vector<int> v[200005];
queue<pair<int, int> > q;
void bfs(int x, int od)
{
q.push({x, od});
odl[x] = od;
while(!q.empty())
{
x = q.front().first;
od = q.front().second;
q.pop();
for(int i = 0; i < (int)v[x].size(); ++i)
{
if(!odl[v[x][i]])
{
odl[v[x][i]] = od + 1;
q.push({v[x][i], od + 1});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; ++i) cin >> t[i];
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j < i; ++j)
{
if(t[i] < t[j])
{
v[i].push_back(j);
v[j].push_back(i);
}
}
}
long long suma;
for(int i = 1; i <= n; ++i)
{
bfs(i, 1);
suma = 0;
for(int j = 1; j <= n; ++j)
{
if(odl[j]) suma += (odl[j] - 1);
odl[j] = 0;
}
cout << suma << " ";
}
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; int t[200005], odl[200005]; vector<int> v[200005]; queue<pair<int, int> > q; void bfs(int x, int od) { q.push({x, od}); odl[x] = od; while(!q.empty()) { x = q.front().first; od = q.front().second; q.pop(); for(int i = 0; i < (int)v[x].size(); ++i) { if(!odl[v[x][i]]) { odl[v[x][i]] = od + 1; q.push({v[x][i], od + 1}); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1; i <= n; ++i) cin >> t[i]; for(int i = 1; i <= n; ++i) { for(int j = 1; j < i; ++j) { if(t[i] < t[j]) { v[i].push_back(j); v[j].push_back(i); } } } long long suma; for(int i = 1; i <= n; ++i) { bfs(i, 1); suma = 0; for(int j = 1; j <= n; ++j) { if(odl[j]) suma += (odl[j] - 1); odl[j] = 0; } cout << suma << " "; } return 0; } |
English