이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// @tarche
// Feast
// OJUZ
// Singapore NOI 2019
// https://oj.uz/problem/view/NOI19_feast
// 2024-01-28
#include <bits/stdc++.h>
#define forn(i, n) for (tint i = 0; i < tint(n); i++)
#define forsn(i, s, n) for (tint i = s; i < tint(n); i++)
#define dforn(i, n) for (tint i = tint(n) - 1; i >= 0; i--)
#define dforsn(i, s, n) for (tint i = tint(n) - 1; i >= s; i--)
#define sz(x) tint(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define DBG(x) cerr << #x << " = " << x << endl
using namespace std;
#ifndef SMALL_TINT
using tint = long long;
#else
using tint = int;
#endif
using vi = vector<tint>;
using pii = pair<tint, tint>;
inline void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
inline string YN(bool x, string y = "YES", string n = "NO") { return (x ? y : n); }
template <typename T>
inline void chmax(T &lhs, T rhs) {
lhs = max(lhs, rhs);
}
template <typename T>
inline void chmin(T &lhs, T rhs) {
lhs = min(lhs, rhs);
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
forn(i, sz(v)) {
if (i > 0) os << ", ";
os << v[i];
}
os << "]";
return os;
}
template <typename T, size_t N>
ostream &operator<<(ostream &os, const array<T, N> &v) {
os << "[";
forn(i, N) {
if (i > 0) os << ", ";
os << v[i];
}
os << "]";
return os;
}
const tint INF = 1e9;
pii solve(const vi &nums, tint lambda) {
tint n = sz(nums);
vector<vector<pii>> dp(n + 1, vector<pii>(2, {-INF, 0}));
dp[0][0].first = 0;
forn(i, n) {
chmax(dp[i + 1][0], dp[i][0]);
chmax(dp[i + 1][0], dp[i][1]);
chmax(dp[i + 1][1], {dp[i][0].first + nums[i] - lambda, dp[i][0].second - 1});
chmax(dp[i + 1][1], {dp[i][1].first + nums[i], dp[i][1].second});
}
pii ans = max(dp[n][0], dp[n][1]);
ans.second = -ans.second;
ans.first += lambda * ans.second;
return ans;
}
int main() {
fastIO();
tint n, k;
cin >> n >> k;
vi nums(n);
forn(i, n) cin >> nums[i];
tint lo = 0, hi = INF;
while (hi - lo > 1) {
tint lambda = (hi + lo) / 2;
if (solve(nums, lambda).second <= k) {
hi = lambda;
} else {
lo = lambda;
}
}
cout << solve(nums, hi).first << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |