# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
891642 | vjudge1 | Meetings (IOI18_meetings) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "meetings.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
vll solve(span<int> V) {
int len = V.size();
stack<int> S;
vll re;
ll cur = 0;
for(int i = 0; i < len; ++i) {
while(!S.empty() && V[i] > V[S.top()]) {
int p = S.top();
S.pop();
int p2 = 0;
if(!S.empty()) p2 = S.top() + 1;
cur -= (p - p2 + 1) * V[p];
}
int p = 0;
if(!S.empty()) p = S.top() + 1;
cur += (i - p + 1) * V[i];
S.push(i);
re.push_back(cur);
}
return re;
}
vll minimum_costs(vi H, vi L, vi R) {
span<int> S(H);
vi RH = H;
reverse(RH.begin(), RH.end());
span<int> RS(RH);
int n = H.size();
vll RE;
if(H.size() <= 5000 && L.size() <= 5000) {
for(int q = 0; q < L.size(); ++q) {
vll opt1 = solve(S.subspan(L[q], R[q] - L[q] + 1));
vll opt2 = solve(RS.subspan(n - 1 - R[q], R[q] - L[q] + 1));
reverse(opt2.begin(), opt2.end());
ll re = opt1[0] + opt2[0];
// for(auto it : opt1)
// cout << it << " ";
// cout << "\n";
// for(auto it : opt2)
// cout << it << " ";
// cout << "\n";
// cout << "\n";
for(int i = 0; i < R[q] - L[q] + 1; i++)
re = min(re, opt1[i] + opt2[i] - H[i + L[q]]);
RE.push_back(re);
}
return RE;
}
return vll(L.size() + 1, -1);
}