이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "meetings.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
const int N = 5e3 + 10;
int n, q;
vector<int> H, L, R;
ll dp[N], ans[N];
int par[N], sz[N];
int Find(int u){
if(par[u] == u) return u;
return par[u] = Find(par[u]);
}
void Merge(int u, int v, ll md){
u = par[u];
v = par[v];
// cerr << "^^ " << sz[u] << ' ' << sz[v] << '\n';
assert(u < v);
// phase 1
for(int i = v - sz[v] + 1; i <= v; i++)
dp[i] += 1ll * sz[u] * md;
// phase 2
for(int i = v - sz[v] + 1; i <= v; i++)
dp[i] = min(dp[i], dp[u] + md * (i - u));
sz[v] += sz[u];
par[u] = v;
}
vector<int> Q[N];
void Solve(){
vector<int> ord(n, 0);
iota(all(ord), 0);
sort(all(ord), [&](int i, int j){ return H[i] < H[j]; });
fill(sz, sz + N, 1);
iota(par,par +N, 0);
fill(dp, dp + N, 0);
for(int i = 0; i < N; i++) Q[i].clear();
////////////////////////////
for(int i = 0; i < q; i++){
int idx = L[i];
for(int j = L[i]; j <= R[i]; j++) if(H[j] > H[idx]) idx = j;
Q[idx].pb(i);
}
for(int i : ord){
for(int q_id : Q[i]){
ans[q_id] = min(ans[q_id], 1ll * H[i] * (i - L[q_id] + 1) + dp[R[q_id] + 1]);
}
Merge(i, i + 1, H[i]);
// cerr << "# " << i << '\n';
// cerr << "!! ";
// for(int i = 0; i <= n; i++)
// cerr << dp[i] << ' ';
// cerr << '\n';
}
}
vector<long long> minimum_costs(vector<int> _H, vector<int> _L, vector<int> _R) {
H = _H; L = _L; R = _R;
n = H.size();
q = L.size();
fill(ans, ans + N, Inf);
Solve();
reverse(all(H));
for(auto &x : L) x = n - 1 - x;
for(auto &x : R) x = n - 1 - x;
L.swap(R);
Solve();
vector<ll> ANS;
for(int i = 0; i < q; i++)
ANS.pb(ans[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... |