이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5;
int n, K, prefix[N + 5];
set<tuple<int, int, int, int>, greater<tuple<int, int, int, int>>> segment;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> K;
for (int i = 1;i <= n; i++) {
int x;
cin >> x;
prefix[i] = prefix[i - 1] + x;
}
int index = 1, cur = 0;
for (int i = 1; i < n; i++) index = cur < prefix[i]*(prefix[n] - prefix[i]) ? i : index, cur = max(cur, prefix[i]*(prefix[n] - prefix[i]));
segment.insert({cur, 1, n, index});
int ans = 0;
vector<int> move;
while (K) {
K--;
auto x = *segment.begin();
segment.erase(segment.begin());
int v = get<0>(x), a = get<1>(x), b = get<2>(x), m = get<3>(x);
ans += v;
move.push_back(m);
if (a == b) continue;
int index = a, cur = 0;
for (int i = a; i <= m; i++) {
int res = (prefix[i] - prefix[a - 1])*(prefix[m] - prefix[i]);
if (cur < res) cur = res, index = i;
}
segment.insert({cur, a, m, index});
index = m + 1, cur = 0;
for (int i = m + 1; i <= b; i++) {
int res = (prefix[i] - prefix[m])*(prefix[b] - prefix[i]);
if (cur < res) cur = res, index = i;
}
segment.insert({cur, m + 1, b, index});
}
cout << ans << '\n';
for (auto&v : move) cout << v << " ";
cout << '\n';
return 0;
}
# | 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... |