이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define all(v) (v).begin(), (v).end()
#define F first
#define S second
#define __builtin_popcount __builtin_popcountll
#define sz(v) ((int) (v).size())
#define uni(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define pii(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
///
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll oo = 1e18 + 7;
const int N = 1e6 + 5;
int n, a[N], nxt[N];
ll dp[N];
struct line
{
ll a, b;
line (const ll &_a, const ll &_b)
{
a = _a;
b = _b;
}
ll eval(const ll &x)
{
return a * x + b;
}
};
deque <line> st;
bool bad(const line &l1, const line &l2, const line &l3)
{
return (long double) (l3.b - l1.b) * (l1.a - l2.a) < (long double) (l2.b - l1.b) * (l1.a - l3.a);
}
void add(const line &x)
{
while (st.size() >= 2 && bad(st[st.size() - 2], st[st.size() - 1], x)) st.pop_back();
st.push_back(x);
}
ll query(ll x)
{
while (st.size() >= 2 && st[0].eval(x) >= st[1].eval(x)) st.pop_front();
return st[0].eval(x);
}
void Input()
{
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
nxt[1] = 1;
for (int i = 2; i <= n; i ++)
if (a[nxt[i - 1]] < a[i]) nxt[i] = i;
else nxt[i] = nxt[i - 1];
}
void solve()
{
add(line(0, 0));
for (int i = 1; i <= n; i ++)
{
if (nxt[i] == i) dp[i] = query(a[i]) + 1LL * n * a[i];
else dp[i] = dp[i - 1];
add(line(-i, dp[i]));
}
cout << dp[n];
}
int main()
{
std::ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("DIAMOND.inp", "r", stdin);
// freopen("DIAMOND.out", "w", stdout);
int test = 1;
//cin >> test;
while (test--)
{
Input();
solve();
}
return 0;
}
# | 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... |