#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
int n; cin>>n;
vector<int> tab;
for(int i = 0; i < n; i++){
int a; cin>>a;
tab.push_back(a-1);
}
for(int i = 0; i < n; i++){
vector<int> cost(n, 1e9);
cost[i] = 0;
queue<int> q;
q.push(i);
while(q.size()){
int cur = q.front();
q.pop();
for(int j = 0; j < n; j++){
if((j<cur&&tab[j]>tab[cur]) || (j>cur&&tab[j]<tab[cur])){
if(cost[j]==1e9){
cost[j] = cost[cur]+1;
q.push(j);
}
}
}
}
long long wyn = 0;
for(int j = 0; j < n; j++){
if(cost[j]!=1e9) wyn+=cost[j];
}
cout<<wyn<<' ';
}
cout<<'\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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; vector<int> tab; for(int i = 0; i < n; i++){ int a; cin>>a; tab.push_back(a-1); } for(int i = 0; i < n; i++){ vector<int> cost(n, 1e9); cost[i] = 0; queue<int> q; q.push(i); while(q.size()){ int cur = q.front(); q.pop(); for(int j = 0; j < n; j++){ if((j<cur&&tab[j]>tab[cur]) || (j>cur&&tab[j]<tab[cur])){ if(cost[j]==1e9){ cost[j] = cost[cur]+1; q.push(j); } } } } long long wyn = 0; for(int j = 0; j < n; j++){ if(cost[j]!=1e9) wyn+=cost[j]; } cout<<wyn<<' '; } cout<<'\n'; return 0; } |
English