이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
using ll = long long;
const int N = 1e5 + 5;
int n, nk, a[N];
namespace sub1 {
const int N = 1e2 + 5, oo = 1e9 + 123;
int dp[N][N];
void sol() {
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= nk; ++j)
dp[i][j] = oo;
dp[0][0] = 0;
for (int i = 1; i <= n; ++i)
for (int k = 1; k <= nk; ++k) {
for (int j = i, mx = 0; j >= 1; --j)
mx = max(mx, a[j]), dp[i][k] = min(dp[i][k], dp[j - 1][k - 1] + mx);
}
cout << dp[n][nk];
}
}
namespace sub2 {
struct state {
ll fi, tr, se;
state() {}
state(ll fi, ll tr, ll se) : fi(fi), tr(tr), se(se) {}
};
const ll oo = 1e15;
ll dp[N][105];
void sol() {
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= nk; ++j)
dp[i][j] = oo;
dp[0][0] = 0;
for (int k = 1; k <= nk; ++k) {
stack<state> que;
a[k - 1] = 1e9; que.emplace(k - 1, dp[k - 1][k - 1], oo);
for (int i = k; i <= n; ++i) {
ll cur = oo, cur2 = oo;
while (a[i] >= a[que.top().fi]) {
cur = min(cur, que.top().tr + a[i]);
cur2 = min(cur2, que.top().tr);
que.pop();
}
cur = min(cur, min(que.top().se, dp[que.top().fi][k - 1] + a[i]));
cur2 = min(cur2, dp[i][k - 1]);
dp[i][k] = cur; que.emplace(i, cur2, cur);
}
}
cout << dp[n][nk];
}
}
int main() {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
if (fopen("Kblocks.inp", "r"))
freopen("Kblocks.inp", "r", stdin),
freopen("Kblocks.out", "w", stdout);
cin >> n >> nk;
for (int i = 1; i <= n; ++i) cin >> a[i];
if (n <= 1e2) sub1::sol();
else sub2::sol();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
blocks.cpp: In function 'int main()':
blocks.cpp:59:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen("Kblocks.inp", "r", stdin),
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:60:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("Kblocks.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |