This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int mxN = 1e5 + 6;
int a[mxN], pre[mxN];
int n, k;
auto sumSegment(int l, int r) {
return pre[r] - (l > 0 ? pre[l - 1] : 0);
}
int getMidPos(int l, int r){
auto findVal = (l > 0 ? sumSegment(0, l - 1) : 0) + sumSegment(l, r) / 2;
if(findVal <= pre[l])
return l;
auto midPos = upper_bound(pre + l, pre + r + 1, findVal) - pre - 1;
assert(midPos >= 0);
auto curDis = abs(sumSegment(midPos + 1, n - 1) - pre[midPos]);
auto nextDis = midPos + 1 < r ? abs(sumSegment(midPos + 2, n - 1) - pre[midPos + 1]) : 1e9;
return midPos + (curDis > nextDis);
}
auto maxPointInSeg(int l, int r){
int midPos = getMidPos(l, r);
int64_t point = 1LL * sumSegment(l, midPos) * sumSegment(midPos + 1, 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() {
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);
// default_file();
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... |