# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
671137 | vuavisao | Feast (NOI19_feast) | C++14 | 126 ms | 10412 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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;
template<typename Lhs, typename Rhs> inline bool Max_self(Lhs &a, Rhs b) { if(b > a) { a = b; return true; } return false; }
template<typename Lhs, typename Rhs> inline bool Min_self(Lhs &a, Rhs b) { if(b < a) { a = b; return true; } return false; }
const ll N = 3e5 + 10;
const ll INF = 1e18;
ll n, k, a[N];
namespace sub6 {
bool check() {
return (n <= 2000);
}
ll dp[2010][2010];
ll res;
void solve() {
for(ll gr = 1; gr <= k; ++ gr) {
for(ll i = 1; i <= n; ++ i) {
Max_self(dp[i][gr], dp[i - 1][gr] + a[i]);
Max_self(dp[i][gr], dp[i - 1][gr - 1] + a[i]);
}
for(ll i = 1; i <= n; ++ i) Max_self(dp[i][gr], dp[i - 1][gr]);
Max_self(res, dp[n][gr]);
}
cout << res;
}
}
namespace last_sub {
ll dp[N], cnt[N];
bool check(ll lamba) {
ll best[2] = {0, 0};
for(ll i = 1; i <= n; ++ i) {
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
if(Max_self(dp[i], best[0] + a[i] - lamba)) {
cnt[i] = cnt[best[1]] + 1;
}
if(Max_self(best[0], dp[i] - a[i])) {
best[1] = i;
}
}
return (cnt[n] <= k);
}
void solve() {
// cout << check(INF) << '\n';
for(ll i = 1; i <= n; ++ i) a[i] += a[i - 1];
ll l = 0, r = INF;
ll lamba = - 1;
while(l <= r) {
ll mid = (l + r) >> 1ll;
if(check(mid)) {
lamba = mid;
r = mid - 1;
}
else l = mid + 1;
}
assert(lamba != - 1);
check(lamba);
// cout << dp[n] << ' ' << cnt[n] << '\n';
cout << dp[n] + lamba * cnt[n];
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen("Feast.inp", "r")) {
freopen("Feast.inp", "r", stdin);
freopen("Feast.out", "w", stdout);
}
cin >> n >> k;
for(ll i = 1; i <= n; ++ i) cin >> a[i];
// if(sub6::check()) {
// sub6::solve();
// return 0;
// }
// assert(false);
last_sub::solve();
return 0;
}
/// Code by vuavisao
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |