# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
261330 | Nightlight | Discharging (NOI20_discharging) | C++14 | 248 ms | 30052 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>
#define pii pair<long long, long long>
#define inter second
#define slope first
using namespace std;
int N;
long long A[1000005], ans = LLONG_MAX;
long long T[1000005];
long long pos[1000005];
long long dp[1000005];
pii CHT[100005];
int sz, pointer;
int p;
inline double isect(pii a, pii b) {return (a.inter - b.inter) / (b.slope - a.slope);}
inline bool bad(pii a, pii b, pii c) {return isect(c, a) < isect(a, b);}
inline long long val(long long X, int id) {return CHT[id].slope * X + CHT[id].inter;}
void insert(long long m, long long b) {
pii L = {m, b};
while(sz > 1 && bad(CHT[sz - 1], CHT[sz], L)) sz--;
CHT[++sz] = L;
}
long long query(long long X) {
long long res = LLONG_MAX;
// cout << X << "\n";
for(int i = 1; i <= sz; i++) {
res = min(res, val(X, i));
// cout << CHT[i].first << ": " << val(X, i) << "\n";
}
// cout << "\n";
return res;
}
int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i++) {
scanf("%lld", &T[i]);
if(T[i] > A[p]) {
A[++p] = T[i];
pos[p] = i;
}
}
dp[p] = A[p] * (N - pos[p] + 1);
ans = A[p] * N;
insert(A[p], dp[p] + A[p] * pos[p]);
for(int i = p - 1; i > 0; i--) {
dp[i] = A[i] * (N - pos[i] + 1) + query(-pos[i + 1]);
insert(A[i], dp[i] + A[i] * pos[i]);
ans = min(ans, dp[i] + A[i] * (pos[i] - 1));
}
printf("%lld\n", ans);
cin >> N;
}
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... |