이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <map>
#include <set>
using namespace std;
struct SparseTableMin {
vector<vector<int64_t>> dp;
int64_t query (int l, int r) {
assert(l <= r);
int sz = log2(r - l + 1);
assert(r - (1 << sz) + 1 < dp.size());
return min(dp[l][sz], dp[r - (1 << sz) + 1][sz]);
}
SparseTableMin (vector<int64_t> a) {
int n = a.size();
dp.resize(n);
for (int i = 0; i < n; i++) {
dp[i].resize(20);
dp[i][0] = a[i];
}
for (int j = 1; j < dp[0].size(); j++) {
for (int i = 0; i < a.size(); i++) {
dp[i][j] = min(dp[i][j - 1], dp[min(i + (1 << (j - 1)), n - 1)][j - 1]);
}
}
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
int64_t arr[N];
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int64_t INF = 1e17;
vector<int64_t> prev(N + 1), cur(N + 1);
prev.assign(N + 1, INF), cur.assign(N + 1, INF);
for (int j = 1; j <= N; j++) {
prev[j] = ((j == 1) ? arr[0] : max(prev[j - 1], arr[j - 1]));
}
for (int i = 2; i <= K; i++) {
vector<int64_t> val;
val.assign(N + 1, INF);
for (int j = 2; j <= N; j++) {
for (int l = j - 1; l >= 0; l--) {
if (arr[l] <= arr[j - 1]) {
val[l] = prev[l] + arr[j - 1];
} else {
break;
}
}
}
for (int j = 1; j <= N; j++) {
if (j == 1) {
val[0] = prev[0] + arr[0];
cur[1] = prev[0] + arr[0];
} else {
cur[j] = INF;
int ind = -1;
for (int l = j - 1; l >= 0; l--) {
if (arr[l] <= arr[j - 1]) {
ind = l;
} else {
break;
}
}
for (int l = j - 1; l >= ind; l--) {
cur[j] = min(cur[j], prev[l]);
}
cur[j] += arr[j - 1];
SparseTableMin st(val);
if (ind != 0) {
cur[j] = min(cur[j], st.query(0, ind - 1));
}
}
}
swap(cur, prev);
}
cout << prev[N];
}
컴파일 시 표준 에러 (stderr) 메시지
In file included from /usr/include/c++/10/cassert:44,
from blocks.cpp:3:
blocks.cpp: In member function 'int64_t SparseTableMin::query(int, int)':
blocks.cpp:13:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | assert(r - (1 << sz) + 1 < dp.size());
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
blocks.cpp: In constructor 'SparseTableMin::SparseTableMin(std::vector<long int>)':
blocks.cpp:23:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int j = 1; j < dp[0].size(); j++) {
| ~~^~~~~~~~~~~~~~
blocks.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |