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>
#define int long long
using namespace std;
int nums[1000001], dp[1000001];
deque<pair<int,int> > dq;
int f(pair<int,int> line, int x){
return line.first*x + line.second;
}
int query(int x){
while(dq.size()>1){ //to prevent seg fault when accessing dq[1]
if(f(dq[0],x)<f(dq[1],x)) //the next line is better
dq.pop_front(); //remove useless line
else break;
}
return f(dq[0],x);
}
double intersect(int m1, int c1, int m2, int c2){
return (double)(c2-c1)/(m1-m2);
}
double intersect(pair<int,int> p1, pair<int,int> p2){
return intersect(p1.first,p1.second,p2.first,p2.second);
}
void insert(int m,int c) {//insert line y=mx+c
pair<int,int> line = make_pair(m,c);
while (dq.size()>1) { //to prevent seg fault
int s = dq.size();
if (intersect(dq[s-1], line) <= intersect(dq[s-2], line))
dq.pop_back(); //removes useless line
else break;
}
dq.push_back(line);
}
main(){
int n; cin >> n;
for (int i = 1; i <= n; i++){
dp[i] = 1e16;
cin >> nums[i];
nums[i] = max(nums[i], nums[i - 1]);
}
for (int i = 1; i <= n; i++){
insert(i, -dp[i - 1]);
dp[i] = -query(nums[i]) + nums[i] * n + nums[i];
/*
for (int j = 1; j <= i; j++){
dp[i] = min(dp[i], - nums[i] * j + dp[j - 1] + nums[i] * n + nums[i]));
}
*/
}
cout << dp[n] << '\n';
}
Compilation message (stderr)
Discharging.cpp:33:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
33 | main(){
| ^
# | 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... |