이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// fest
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)
typedef long long ll;
typedef long double ld;
using namespace std;
void FREOPEN() {
#ifdef COMP
freopen(".in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
}
inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }
const int N = 10001, M = 201, inf = 1e9 * 2, MOD = (int)1e9 + 7;
char CH[N];
const double eps = 1e-14;
const ll INF = 1e18;
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
int pref[N], a[N], pr[N][M];
ll dp[N][M];
double meet(int a1, ll b1, int a2, ll b2) {
return (double)(b2 - b1) / (double)(a1 - a2);
}
int main() {
FREOPEN();
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d ", a + i);
for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i];
for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -INF, pr[i][j] = -1;
dp[0][0] = 0;
vector<pair<int, pair<ll, int> > > lines;
for (int bl = 1; bl <= k; bl++) {
lines.clear();
lines.pb({-pref[bl - 1], {dp[bl - 1][bl - 1], bl - 1}});
int lef = 0;
for (int i = bl; i <= n; i++) {
int x = pref[n] - pref[i];
while (lef + 1 < SZ(lines) && x * 1ll * lines[lef].F + lines[lef].S.F < x * 1ll * lines[lef + 1].F + lines[lef + 1].S.F) lef++;
dp[i][bl] = x * 1ll * pref[i] + x * 1ll * lines[lef].F + lines[lef].S.F;
pr[i][bl] = lines[lef].S.S;
while (SZ(lines) >= 2 && meet(lines.back().F, lines.back().S.F, -pref[i], dp[i][bl - 1]) > meet(lines[SZ(lines) - 2].F, lines[SZ(lines) - 2].S.F, -pref[i], dp[i][bl - 1]) - eps) lines.pop_back();
while (!lines.empty() && lines.back().F == -pref[i] && lines.back().S.F == dp[i][bl - 1]) lines.pop_back();
if (lef >= SZ(lines)) lef = SZ(lines) - 1;
lines.pb({-pref[i], {dp[i][bl - 1], i}});
}
}
int pos = 0;
ll ans = -INF;
for (int i = 1; i <= n; i++) {
if (ans < dp[i][k]) {
ans = dp[i][k];
pos = i;
}
}
vector<int> out;
cout << ans << endl;
int bl = k;
while (pos > 0) {
out.pb(pos);
pos = pr[pos][bl];
bl--;
}
sort(All(out));
for (auto i : out) printf("%d ", i);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:53:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%d ", a + i);
^
# | 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... |