Submission #965078

# Submission time Handle Problem Language Result Execution time Memory
965078 2024-04-18T05:11:07 Z 12345678 Cat Exercise (JOI23_ho_t4) C++17
0 / 100
1 ms 4444 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int nx=2e5+5;

ll n, a[nx], x, y, rt, l[nx], r[nx], dp[nx];
stack<pair<int, int>> s;

void dfs(int u)
{
    if (l[u]) dfs(l[u]), dp[u]=max(dp[u], dp[l[u]]+u-l[u]);
    if (r[u]) dfs(r[u]), dp[u]=max(dp[u], dp[r[u]]+r[u]-u);
    //cout<<"dp "<<u<<' '<<dp[u]<<'\n';
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n; i++) cin>>a[i];
    for (int i=1; i<n; i++) cin>>x>>y;
    for (int i=1; i<=n; i++)
    {
        if (s.empty()) s.push({a[i], i});
        else
        {
            while (!s.empty()&&s.top().first<a[i])
            {
                l[i]=s.top().second;
                s.pop();
            }           
            if (!s.empty()) r[s.top().second]=i;
            else rt=i;
            s.push({a[i], i});
        }
    }
    //for (int i=1; i<=n; i++) cout<<"debug "<<i<<' '<<l[i]<<' '<<r[i]<<'\n';
    dfs(rt);
    cout<<dp[rt];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Incorrect 1 ms 4444 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Incorrect 1 ms 4444 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Incorrect 1 ms 4444 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Incorrect 1 ms 4444 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Incorrect 1 ms 4444 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Incorrect 1 ms 4444 KB Output isn't correct
5 Halted 0 ms 0 KB -