# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
575903 | JovanB | Discharging (NOI20_discharging) | C++17 | 114 ms | 8212 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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1000000;
const ll INF = 1000000000000000000LL;
ll a[N+5];
ll dp[N+5];
struct line{
ll k, n;
ll eval(int x){ return k*x + n; }
ld intersect(line h){
return (ld)(n - h.n)/(h.k - k);
}
};
deque <line> dq;
void dodaj(line g){
while(dq.size() >= 2){
line b = dq.back();
dq.pop_back();
line a = dq.back();
dq.pop_back();
if(a.intersect(g) > a.intersect(b)) break;
dq.push_back(a);
}
dq.push_back(g);
}
ll query(int x){
while(dq.size() >= 2 && dq[0].eval(x) >= dq[1].eval(x)) dq.pop_front();
return dq.front().eval(x);
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
}
reverse(a+1, a+1+n);
vector <pair <int, int>> vec;
for(int i=n; i>=1; i--){
if(vec.empty() || vec.back().second < a[i]){
vec.push_back({i, a[i]});
}
}
reverse(vec.begin(), vec.end());
for(int i=0; i<vec.size(); i++){
int id, h;
tie(id, h) = vec[i];
line g;
g.k = a[id];
g.n = 0;
if(i) g.n = dp[i-1];
dodaj(g);
dp[i] = query(id);
}
cout << dp[vec.size() - 1] << "\n";
return 0;
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |