Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <iostream> // #include <vector> // #include <queue> #include <bits/stdc++.h> using namespace std; // Numery lin od 0 do n - 1. // Przeszukiwanie wszerz. Wype�nia dist[]. void BFS(vector<int> adj[], int src, int dist[], int n) { bool *visited = new bool[n]; for (int i = 0; i < n; i++) visited[i] = false; dist[src] = 0; queue<int> q; q.push(src); visited[src] = true; while (!q.empty()) { int curr = q.front(); q.pop(); for (vector<int>::iterator it = adj[curr].begin(); it != adj[curr].end(); it++) { int x = *it; if (visited[x] == false) { q.push(x); visited[x] = true; } if (dist[x] > dist[curr] + 1) dist[x] = dist[curr] + 1; } } delete [] visited; } int main() { int n; cin >> n; int *p = new int[n]; int *sum = new int[n]; for (int i = 0; i < n; i++) cin >> p[i]; // Kt�re si� przecinaj�? vector<int> *adj = new vector<int>[n]; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (p[j] < p[i]) { adj[i].push_back(j); adj[j].push_back(i); } // Obliczenie sum. int *dist = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) dist[j] = INT_MAX; BFS(adj, i, dist, n); sum[i] = 0; for (int j = 0; j < n; j++) if (dist[j] <= n) sum[i] += dist[j]; } int ilast = n - 1; for (int i = 0; i < ilast; i++) cout << sum[i] << " "; cout << sum[ilast] << endl; delete [] adj; delete [] p; delete [] dist; delete [] sum; // system("pause"); }
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 | #include <iostream> // #include <vector> // #include <queue> #include <bits/stdc++.h> using namespace std; // Numery lin od 0 do n - 1. // Przeszukiwanie wszerz. Wype�nia dist[]. void BFS(vector<int> adj[], int src, int dist[], int n) { bool *visited = new bool[n]; for (int i = 0; i < n; i++) visited[i] = false; dist[src] = 0; queue<int> q; q.push(src); visited[src] = true; while (!q.empty()) { int curr = q.front(); q.pop(); for (vector<int>::iterator it = adj[curr].begin(); it != adj[curr].end(); it++) { int x = *it; if (visited[x] == false) { q.push(x); visited[x] = true; } if (dist[x] > dist[curr] + 1) dist[x] = dist[curr] + 1; } } delete [] visited; } int main() { int n; cin >> n; int *p = new int[n]; int *sum = new int[n]; for (int i = 0; i < n; i++) cin >> p[i]; // Kt�re si� przecinaj�? vector<int> *adj = new vector<int>[n]; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (p[j] < p[i]) { adj[i].push_back(j); adj[j].push_back(i); } // Obliczenie sum. int *dist = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) dist[j] = INT_MAX; BFS(adj, i, dist, n); sum[i] = 0; for (int j = 0; j < n; j++) if (dist[j] <= n) sum[i] += dist[j]; } int ilast = n - 1; for (int i = 0; i < ilast; i++) cout << sum[i] << " "; cout << sum[ilast] << endl; delete [] adj; delete [] p; delete [] dist; delete [] sum; // system("pause"); } |