이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define MAX 100100
#define MAX_K 210
#define MOD 1000000007
#define INF 0x7f7f7f7f7f7f7f7f
#define endl '\n'
using namespace std;
typedef pair<int, int> pr;
typedef array<int, 3> tp;
typedef array<int, 4> ftp;
class ConvexHullTrick {
public:
vector<ftp> F;
int ftop = 0;
void insert(tp X) {
ftp K = {X[0], X[1], 0, X[2]};
while (!F.empty()) {
if (F.back()[0] == K[0]) {
if (F.back()[1] >= K[1])
return;
} else {
K[2] = (F.back()[1] - K[1]) / (K[0] - F.back()[0]);
if (F.back()[2] < K[2])
break;
}
F.pop_back();
if (F.size() == ftop)
--ftop;
}
F.push_back(K);
}
tp query(int x) {
while (ftop + 1 < F.size() && F[ftop + 1][2] < x)
++ftop;
return {F[ftop][0] * x + F[ftop][1], F[ftop][3]};
}
void clear() {
F.clear();
ftop = 0;
}
};
int A[MAX], dp[MAX], sm[MAX], track[MAX][MAX_K];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int N, K, res;
tp val;
vector<int> ans;
ConvexHullTrick cht[2];
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> A[i];
sm[i] = sm[i - 1] + A[i];
}
cht[1].insert({0, 0});
for (int i = 0; i <= K; i++) {
cht[i & 1].clear();
cht[i & 1].insert({0, 0});
for (int j = i + 1; j <= N; j++) {
val = (j ? cht[(i + 1) & 1].query(sm[j]) : tp{0, 0});
dp[j] = sm[j] * sm[j] + val[0];
track[j][i] = val[1];
cht[i & 1].insert({-2 * sm[j], dp[j] + sm[j] * sm[j], j});
}
}
res = (sm[N] * sm[N] - dp[N]) / 2;
cout << res << '\n';
while (K--) {
N = track[N][K];
ans.push_back(N);
}
reverse(ans.begin(), ans.end());
for (int i : ans)
cout << i + 1 << ' ';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'void ConvexHullTrick::insert(tp)':
sequence.cpp:32:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
32 | if (F.size() == ftop)
| ~~~~~~~~~^~~~~~~
sequence.cpp: In member function 'tp ConvexHullTrick::query(long long int)':
sequence.cpp:39:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | while (ftop + 1 < F.size() && F[ftop + 1][2] < x)
| ~~~~~~~~~^~~~~~~~~~
# | 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... |