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;
std::vector<long long> minimum_costs(std::vector<int> arr, std::vector<int> L,
std::vector<int> R) {
int m = (int)L.size();
int n = (int)arr.size();
vector<long long>ans(m);
vector<long long>pref(n + 1),suff(n + 1);
priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>>q;
vector<long long>pref_cnt(n + 1,0),suff_cnt(n + 1,0);
for (long long i = 0;i < n;++i){
pref[i + 1] = pref[i] + arr[i];
long long counts = 1;
while(!q.empty() && q.top().first < arr[i]){
pref[i + 1] += (arr[i] - q.top().first)*q.top().second;
pref_cnt[i + 1] +=(arr[i] - q.top().first) * q.top().second;
counts+=q.top().second;
q.pop();
}
q.push({arr[i],counts});
}
while(!q.empty())q.pop();
for (long long i = n - 1;i>=0;--i){
suff[i] = suff[i + 1] + arr[i];
long long counts = 1;
while(!q.empty() && q.top().first < arr[i]){
suff[i]+=(arr[i] - q.top().first) * q.top().second;
suff_cnt[i]+=(arr[i] - q.top().first) * q.top().second;
counts+=q.top().second;
q.pop();
}
q.push({arr[i],counts});
}
for (long long i = 1;i<=n;++i){
pref_cnt[i] +=pref_cnt[i - 1];
}
for (long long i = n - 1;i>=0;--i){
suff_cnt[i] +=suff_cnt[i + 1];
}
for (long long i = 0;i<m;++i){
long long answer = LLONG_MAX;
for (long long j = L[i];j<=R[i];++j){
//cout<<i<<" "<<pref[j]<<" "<<suff[j]<<'\n';
answer = min(answer,pref[j + 1] + suff[j] - arr[j] - pref[L[i]] - suff[R[i] + 1] - pref_cnt[L[i] + 1] - suff_cnt[R[i] + 1]);
}
ans[i] = answer;
}
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... |