# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1278640 | ducdev | Discharging (NOI20_discharging) | C++17 | 83 ms | 27828 KiB |
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 1e6;
const int MOD = 1e9 + 7;
struct Line {
ll a, b;
Line() {};
Line(ll a, ll b) : a(a), b(b) {};
double intersect(const Line &other) const {
return (double)(other.b - b) / (a - other.a);
};
ll eval(ll x) {
return a * x + b;
};
};
struct ConvexHullTrick {
Line lines[MAX_N + 5];
int ptr, sz;
void addLine(const Line &newLine) {
while (sz > 1 && lines[sz - 2].intersect(newLine) <= lines[sz - 2].intersect(lines[sz - 1])) sz--;
lines[sz++] = newLine;
if (ptr >= sz) ptr = max(0, sz - 1);
};
ll query(ll x) {
while (ptr + 1 < sz && lines[ptr].eval(x) >= lines[ptr + 1].eval(x)) ptr++;
return lines[ptr].eval(x);
};
void reset() { sz = ptr = 0; };
ConvexHullTrick() { reset(); };
};
int n;
int a[MAX_N + 5];
ll dp[MAX_N + 5];
ConvexHullTrick cht;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("MAIN.INP", "r")) {
freopen("MAIN.INP", "r", stdin);
freopen("MAIN.OUT", "w", stdout);
};
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
};
ConvexHullTrick cht;
int mx = 0;
for (int i = 1; i <= n; i++) {
cht.addLine(Line(-i, dp[i - 1]));
if (a[i] > mx) {
mx = a[i];
dp[i] = cht.query(mx) + 1LL * mx * (n + 1);
} else
dp[i] = dp[i - 1];
};
cout << dp[n];
};
컴파일 시 표준 에러 (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... |