# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1162350 | blackslex | K개의 묶음 (IZhO14_blocks) | C++20 | 118 ms | 3288 KiB |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
int n, k;
int main() {
scanf("%d %d", &n, &k);
vector<int> a(n + 5);
vector<vector<ll>> dp(2, vector<ll>(n + 5));
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) dp[1][i] = max(dp[1][i - 1], (ll) a[i]);
for (int i = 2; i <= k; i++) {
for (int j = 0; j <= n; j++) dp[i & 1][j] = 0;
stack<pii> st;
for (int j = i; j <= n; j++) {
ll cmn = dp[!(i & 1)][j - 1];
while (!st.empty() && a[st.top().first] <= a[j]) {
cmn = min(cmn, st.top().second);
st.pop();
}
dp[i & 1][j] = cmn + a[j];
if (!st.empty()) dp[i & 1][j] = min(dp[i & 1][j], dp[i & 1][st.top().first]);
st.emplace(j, cmn);
}
for (int j = 0; j < i; j++) dp[i & 1][j] = 1e18;
}
printf("%lld", dp[k & 1][n]);
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |