이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define N 100005
#define oo 1e15
#define int long long
typedef pair<int, long long> ii;
typedef pair<int, pair<long long, long long>> iii;
struct cht{
deque<iii> q;
long long get(ii tmp, long long x){
return tmp.fi * x + tmp.se;
}
bool cal(ii a, ii b, ii c){
return ((long long) 1ll * (c.se - a.se) * (b.fi - c.fi) >= 1ll * (c.se - b.se) * (a.fi - c.fi));
}
void add(long long a, long long b, int pos){
iii nw = {pos, {a, b}};
while ((int) q.size() >= 2 && cal(q[(int)q.size() - 2].se, q[(int)q.size() - 1].se, nw.se)) q.pop_back();
q.push_back(nw);
}
ii get(long long pos){
if (!(int) q.size()) return {0, 0};
while ((int) q.size() >= 2 && get(q[0].se, pos) <= get(q[1].se, pos)) q.pop_front();
return {q[0].fi, get(q[0].se, pos)};
}
void reset(){
q.clear();
}
} c1;
long long pre[N], suf[N];
int par[201][N], n, k;
vector<long long> cur, lst;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> pre[i];
pre[i] = pre[i - 1] + pre[i];
}
for (int i = 1; i <= n; i++){
suf[i] = pre[n] - pre[i - 1];
}
cur.resize(n + 1, 0);
lst.resize(n + 1, 0);
for (int i = 1; i <= n; i++){
cur[i] = pre[i] * suf[i + 1];
}
for (int i = 2; i <= k; i++){
lst = cur;
c1.reset();
for (int j = 1; j < n; j++){
ii gt = c1.get(-suf[j + 1]);
par[i][j] = gt.fi;
cur[j] = gt.se + suf[j + 1] * pre[j];
c1.add(pre[j], lst[j], j);
}
}
long long res = -oo; int pos = 0, nn = k;
for (int i = 1; i < n; i++){
if (cur[i] > res){
res = cur[i];
pos = i;
}
}
vector<int> vec;
while (pos != 0){
vec.push_back(pos);
pos = par[k][pos];
k--;
}
sort(vec.begin(), vec.end());
cout << res << "\n";
for (auto x : vec) cout << x << " ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:71:39: warning: unused variable 'nn' [-Wunused-variable]
71 | long long res = -oo; int pos = 0, nn = k;
| ^~
# | 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... |