이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("arch=skylake")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k, a[mxN];
ll pref[mxN], ans;
pair<ll, int> dp[mxN];
ll calc(int l, int r) {
if(l > r) return 0;
return pref[r] - pref[l - 1];
}
void solve() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
ll l = -oo / n, r = 0;
while(l < r) {
ll mid = (l + r + 1) >> 1;
pair<ll, int> maxx = {0, 0};
for(int i = 1; i <= n; ++i) {
ll tmp = maxx.ff + pref[i] + mid;
if(dp[i - 1].ff == tmp) dp[i] = min(dp[i - 1], pair{tmp, maxx.ss + 1});
else dp[i] = max(dp[i - 1], pair{tmp, maxx.ss + 1});
if(maxx.ff == dp[i].ff - pref[i]) maxx = min(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
else maxx = max(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
}
if(maxx.ss <= k) l = mid;
else r = mid - 1;
}
pair<ll, int> maxx = {0, 0}, doesntexist = {0, 0};
for(int i = 1; i <= n; ++i) {
ll tmp = maxx.ff + pref[i] + l;
if(dp[i - 1].ff == tmp) dp[i] = min(dp[i - 1], pair{tmp, maxx.ss + 1});
else dp[i] = max(dp[i - 1], pair{tmp, maxx.ss + 1});
if(maxx.ff == dp[i].ff - pref[i]) maxx = min(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
else maxx = max(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
if(dp[i].ff == doesntexist.ff) doesntexist = min(doesntexist, dp[i]);
else doesntexist = max(doesntexist, dp[i]);
}
cout << doesntexist.ff - doesntexist.ss * l << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# | 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... |