Submission #1040152

#TimeUsernameProblemLanguageResultExecution timeMemory
1040152TAhmed33Cat Exercise (JOI23_ho_t4)C++98
0 / 100
1 ms6748 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 25; int a[MAXN], n; int nxt[MAXN], prv[MAXN]; vector <int> adj[MAXN]; int dfs (int pos, int l, int r) { if (adj[pos].empty()) return 0; if (int(adj[pos].empty())) { if (adj[pos][0] < pos) { return dfs(adj[pos][0], l, pos - 1) + pos - adj[pos][0]; } else { return dfs(adj[pos][0], pos + 1, r) + adj[pos][0] - pos; } } else { return max(dfs(adj[pos][0], l, pos - 1) + pos - adj[pos][0], dfs(adj[pos][1], pos + 1, r) + adj[pos][1] - pos); } } void solve () { cin >> n; a[0] = 1e9; for (int i = 1; i <= n; i++) { cin >> a[i]; } stack <int> cur; for (int i = 1; i <= n; i++) { while (!cur.empty() && a[i] > a[cur.top()]) { nxt[cur.top()] = i; cur.pop(); } cur.push(i); } while (!cur.empty()) cur.pop(); for (int i = n; i >= 1; i--) { while (!cur.empty() && a[i] > a[cur.top()]) { prv[cur.top()] = i; cur.pop(); } cur.push(i); } while (!cur.empty()) cur.pop(); for (int i = 1; i <= n; i++) { if (a[nxt[i]] < a[prv[i]]) { adj[nxt[i]].push_back(i); } else { adj[prv[i]].push_back(i); } } adj[0].clear(); int root = -1; for (int i = 1; i <= n; i++) { if (a[i] == n) { root = i; } } cout << dfs(root, 1, n) << '\n'; } signed main () { ios::sync_with_stdio(0); cin.tie(0); int tc = 1; //cin >> tc; while (tc--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...