이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "meetings.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <stack>
typedef long long llong;
const int MAXN = 100000 + 10;
const int INF = 1e9;
int n, q;
int a[MAXN];
llong prefix[MAXN];
llong suffix[MAXN];
std::stack <std::pair <int, llong>> st;
std::vector <llong> ans;
llong solveFor(int l, int r)
{
if (l == r)
{
return a[l];
}
while (!st.empty()) st.pop();
for (int i = l ; i <= r ; ++i)
{
while (!st.empty() && a[st.top().first] < a[i])
{
st.pop();
}
if (st.empty())
{
st.push({i, 1LL * (i - l + 1) * a[i]});
} else
{
st.push({i, st.top().second + 1LL * (i - st.top().first) * a[i]});
}
prefix[i] = st.top().second;
}
while (!st.empty()) st.pop();
for (int i = r ; i >= l ; --i)
{
while (!st.empty() && a[st.top().first] < a[i])
{
st.pop();
}
if (st.empty())
{
st.push({i, 1LL * (r - i + 1) * a[i]});
} else
{
st.push({i, st.top().second + 1LL * (st.top().first - i) * a[i]});
}
suffix[i] = st.top().second;
}
llong ans = suffix[l];
for (int i = l + 1 ; i <= r ; ++i)
{
ans = std::min(ans, prefix[i - 1] + suffix[i]);
}
return ans;
}
std::vector <llong> minimum_costs(std::vector<int> H, std::vector<int> L, std::vector<int> R)
{
n = H.size();
for (int i = 0 ; i < n ; ++i)
{
a[i] = H[i];
}
q = L.size();
ans.resize(q);
for (int i = 0 ; i < q ; ++i)
{
ans[i] = solveFor(L[i], R[i]);
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |