#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
vector<int> find_next_index(const vector<int> &nums)
{
int n = nums.size();
vector<int> result(n, -1);
stack<int> s;
for (int i = 0; i < 2 * n; ++i)
{
int current_idx = i % n;
int current_val = nums[current_idx];
while (!s.empty() && current_val > nums[s.top()])
{
int smaller_index = s.top();
s.pop();
result[smaller_index] = current_idx;
}
if (i < n)
{
s.push(current_idx);
}
}
return result;
}
int count_topological_layers(const vector<int> &data)
{
int n = data.size();
vector<vector<int>> adj(n);
vector<int> in_degree(n, 0);
for (int i = 0; i < n; ++i)
{
int target = data[i];
if (target != -1)
{
adj[i].push_back(target);
in_degree[target]++;
}
}
queue<int> q;
for (int i = 0; i < n; ++i)
{
if (in_degree[i] == 0)
{
q.push(i);
}
}
int layers = 0;
while (!q.empty())
{
layers++;
int level_size = q.size();
for (int i = 0; i < level_size; ++i)
{
int current = q.front();
q.pop();
for (int neighbor : adj[current])
{
in_degree[neighbor]--;
if (in_degree[neighbor] == 0)
{
q.push(neighbor);
}
}
}
}
return layers;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
vector<int> next_indices = find_next_index(a);
cout << count_topological_layers(next_indices) << endl;
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 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 | #include <iostream> #include <vector> #include <stack> #include <queue> using namespace std; vector<int> find_next_index(const vector<int> &nums) { int n = nums.size(); vector<int> result(n, -1); stack<int> s; for (int i = 0; i < 2 * n; ++i) { int current_idx = i % n; int current_val = nums[current_idx]; while (!s.empty() && current_val > nums[s.top()]) { int smaller_index = s.top(); s.pop(); result[smaller_index] = current_idx; } if (i < n) { s.push(current_idx); } } return result; } int count_topological_layers(const vector<int> &data) { int n = data.size(); vector<vector<int>> adj(n); vector<int> in_degree(n, 0); for (int i = 0; i < n; ++i) { int target = data[i]; if (target != -1) { adj[i].push_back(target); in_degree[target]++; } } queue<int> q; for (int i = 0; i < n; ++i) { if (in_degree[i] == 0) { q.push(i); } } int layers = 0; while (!q.empty()) { layers++; int level_size = q.size(); for (int i = 0; i < level_size; ++i) { int current = q.front(); q.pop(); for (int neighbor : adj[current]) { in_degree[neighbor]--; if (in_degree[neighbor] == 0) { q.push(neighbor); } } } } return layers; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> next_indices = find_next_index(a); cout << count_topological_layers(next_indices) << endl; return 0; } |
English