이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <limits.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
long long dv(long long a, long long b) { return a / b /*- ((a^b)<0 && a%b)*/; }
struct Line {
long long a, b; int id;
Line(long long _a, long long _b, int _id) : a(_a), b(_b), id(_id) {}
long long intersect(Line L) { return dv(L.b-b, a-L.a); }
// double intersect(const Line& L) const { return (double)(L.b-b) / (a-L.a); }
long long eval(long long x) { return a*x + b; }
};
struct CHT {
deque<Line> cht;
void add(Line L) {
if(cht.size() && cht.front().a == L.a) {
if(cht.front().b > L.b) return;
cht.pop_front();
}
while(cht.size() >= 2 && L.intersect(cht[1]) < L.intersect(cht[0]))
cht.pop_front();
cht.push_front(L);
}
Line query(int64_t x) {
/* Line ans(-1, -1, -1);
for(Line l : cht)
if(l.eval(x) > ans.eval(x)) ans = l;
return ans; */
while(cht.size() >= 2 && cht[ cht.size() - 2 ].eval(x) >= cht.back().eval(x))
cht.pop_back();
return cht.back();
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
int64_t arr[n]; int64_t pref[n + 1]; pref[0] = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
pref[i + 1] = pref[i] + arr[i];
}
vector<int64_t> dp_cur(n - 1), dp_prev(n - 1);
int prev[n - 1][k + 1];
for (int i = 0; i < n - 1; i++) {
dp_prev[i] = (pref[i + 1]) * (pref[n] - pref[i + 1]);
prev[i][1] = -1;
}
for (int j = 2; j <= k; j++) {
CHT cht;
dp_cur[0] = 0;
for (int i = 1; i < n - 1; i++) {
cht.add(Line(pref[i], dp_prev[i - 1], i));
Line b = cht.query(pref[i + 1] - pref[n]);
dp_cur[i] = max(b.eval(pref[i + 1] - pref[n]), 0ll) + pref[i + 1] * pref[n] - pref[i + 1] * pref[i + 1];
prev[i][j] = b.id - 1;
}
dp_prev = dp_cur;
}
int64_t myMax = 0;
for (int i = 0; i < n - 1; i++) {
myMax = max(myMax, dp_prev[i]);
}
cout << myMax << '\n';
for (int i = 0; i < n - 1; i++) {
if (myMax == dp_prev[i]) {
int mid = i;
int cntr = 0;
while (cntr != k) {
cout << mid + 1 << ' ';
mid = prev[mid][k - cntr];
cntr++;
}
return 0;
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp:14: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
14 | #pragma GCC optimization ("O3")
|
sequence.cpp:15: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
15 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |