이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct Line{
ll m, c;
Line(ll _m, ll _c){
m=_m;c=_c;
}
ll f(ll x){
return m*x+c;
}
ld intersectX(Line l){
return (1.0*l.c-c)/(m-l.m);
}
};
deque<Line> hull;
bool isBetter(Line l){
return (l.intersectX(hull[hull.size()-1])<l.intersectX(hull[hull.size()-2]));
}
void add(Line l){
if (hull.size()>0&&hull.back().m==l.m){
if (l.c<hull.back().c){
hull.pop_back();
hull.push_back(l);
}
return;
}
while (hull.size()>1&&isBetter(l)){
hull.pop_back();
}
hull.push_back(l);
}
ll query(ll x){
while (hull.size()>1&&hull[0].f(x)>hull[1].f(x)){
hull.pop_front();
}
return hull.front().f(x);
}
int main(){
ll n;
cin>>n;
add(Line(n,0));
ll prev = 0;
ll ans;
for (ll i = 1; i<=n; i++){
ll val;
cin>>val;
if (val<prev){
continue;
}
/*cout<<"val: "<<val<<'\n';
for (auto h : hull){
cout<<h.m<<' '<<h.c<<'\n';
}*/
prev=val;
ans = query(val);
add(Line{n-i,ans});
//cout<<ans<<'\n';
}
cout<<ans<<'\n';
}
# | 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... |