이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i <= (b); ++(i))
#define all(x) x.begin(), x.end()
const int mxN = 1e5 + 6;
int a[mxN], pre[mxN];
auto sumSegment(int l, int r) {
return pre[r] - (l > 0 ? pre[l - 1] : 0);
}
auto getMidPos(int l, int r){
return lower_bound(pre + l, pre + r + 1, (l > 0 ? sumSegment(0, l - 1) : 0) + (sumSegment(l, r) / 2)) - pre;
}
auto maxPointInSeg(int l, int r){
int midPos = getMidPos(l, r);
int64_t point = 1LL * sumSegment(l, midPos) * sumSegment(midPos + 1, r);
assert(l != r);
return point;
}
struct Segment{
int l, r;
Segment(int _l, int _r): l(_l), r(_r) {}
bool operator < (const Segment& other) const{
return maxPointInSeg(l, r) > maxPointInSeg(other.l, other.r);
}
};
void solve() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n; ++i){
cin >> a[i];
pre[i] = (i > 0 ? pre[i-1] : 0) + a[i];
}
set<Segment> val;
vector<int> pos;
val.insert(Segment(0, n - 1));
int64_t ans = 0;
for(int i = 1; i <= k; ++i){
auto [l, r] = *val.begin();
val.erase(val.begin());
ans += maxPointInSeg(l, r);
auto midPos = getMidPos(l, r);
pos.push_back(midPos+1);
if(midPos > l) val.insert(Segment(l, midPos));
if(midPos + 1 < r) val.insert(Segment(midPos + 1, r));
}
cout << ans << "\n";
for(auto& p : pos)
cout << p << ' ';
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
solve();
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... |