#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define vec vector #define pb push_back #define pint pair<int, int> #define f first #define s second using namespace std; #define N 1000 #define inf 1000000000 int n; pint nodes[N]; vec<int> graph[N]; bool visited[N]; int bfs_queue[N]; int queue_begin; int queue_end; int d[N]; int bfs(int x){ queue_begin=0; queue_end=1; bfs_queue[0]=x; d[x]=0; foru(i, n) visited[i]=false; visited[x]=true; int ans=0; while(queue_begin != queue_end){ int node = bfs_queue[queue_begin]; queue_begin++; ans += d[node]; for(auto i : graph[node]){ if(visited[i]) continue; visited[i]=true; d[i]=d[node]+1; bfs_queue[queue_end]=i; queue_end++; } } return ans; } int main() { cin >> n; foru(i, n) {nodes[i].f=i; cin >> nodes[i].s; nodes[i].s--;} foru(i, n) foru(j, n){ if(i==j) continue; if((nodes[i].f-nodes[j].f)*(nodes[i].s-nodes[j].s)<0){ graph[i].pb(j); } } foru(i, n) cout << bfs(i) << " "; 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 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define vec vector #define pb push_back #define pint pair<int, int> #define f first #define s second using namespace std; #define N 1000 #define inf 1000000000 int n; pint nodes[N]; vec<int> graph[N]; bool visited[N]; int bfs_queue[N]; int queue_begin; int queue_end; int d[N]; int bfs(int x){ queue_begin=0; queue_end=1; bfs_queue[0]=x; d[x]=0; foru(i, n) visited[i]=false; visited[x]=true; int ans=0; while(queue_begin != queue_end){ int node = bfs_queue[queue_begin]; queue_begin++; ans += d[node]; for(auto i : graph[node]){ if(visited[i]) continue; visited[i]=true; d[i]=d[node]+1; bfs_queue[queue_end]=i; queue_end++; } } return ans; } int main() { cin >> n; foru(i, n) {nodes[i].f=i; cin >> nodes[i].s; nodes[i].s--;} foru(i, n) foru(j, n){ if(i==j) continue; if((nodes[i].f-nodes[j].f)*(nodes[i].s-nodes[j].s)<0){ graph[i].pb(j); } } foru(i, n) cout << bfs(i) << " "; return 0; } |