이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
const ll INF = 10000000000;
struct line {
ll k, b, id;
ll eval(ll x) {
return x * k + b;
}
};
string to_string(line x) {
pair<ll, ll> _;
_.first = x.k, _.second = x.b;
return to_string(_);
}
long double crossX(line a, line b) {
long double num = a.b - b.b;
long double den = b.k - a.k;
if (den == 0) {
return -1000000000;
}
return (long double)(num / den);
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
vector<ll> pref(n + 1);
vector<vector<ll>> dp(k + 1, vector<ll>(n + 1));
vector<vector<int>> last(k + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
for (int j = 0; j <= k; j++)
dp[j][i] = -INF;
}
deque<line> dq;
for (int i = 1; i <= n; i++) {
dp[1][i] = pref[i] * pref[n] - pref[i] * pref[i];
last[1][i] = n + 1;
}
for (int j = 2; j <= k; j++) {
dq.clear();
for (int i = n; i >= 1; i--) {
int idx = -1, mx = -100000000;
for (int ii = 0; ii < sz(dq); ii++) {
if (dq[ii].eval(pref[i]) - pref[i] * pref[i] > mx) {
mx = dq[ii].eval(pref[i]) - pref[i] * pref[i];
idx = ii;
}
}
if (idx != -1) {
dp[j][i] = dq[idx].eval(pref[i]) - pref[i] * pref[i];
last[j][i] = dq[idx].id;
}
if (dp[j - 1][i] == -INF)
continue;
line cur = {pref[i], dp[j - 1][i], i};
while (sz(dq) >= 2 && crossX(cur, dq[0]) >= crossX(dq[0], dq[1]))
dq.pop_front();
dq.push_front(cur);
}
}
ll ans = dp[k][1], cur = 1;
for (int i = 1; i <= n; i++) {
if (dp[k][i] > ans)
ans = dp[k][i], cur = i;
}
cout << ans << '\n';
for (int i = k; i >= 1; i--) {
if (cur > n || cur < 1)
break;
cout << cur << ' ';
cur = last[i][cur];
}
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void setIO(const string&)':
sequence.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |