이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 1e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int n, k, a[N];
namespace Subtask1{
bool ckSub(){
return n <= 1e2;
}
const int N = 105, K = 105;
int dp[K][N];
void process()
{
FOR(i, 0, k){
FOR(j, 0, n){
dp[i][j] = 1e9 + 1;
}
}
dp[0][0] = 0;
FOR(i, 1, k){
FOR(r, 1, n){
int Mx = a[r];
for(int l = r; l >= 1; l--){
ckmx(Mx, a[l]);
ckmn(dp[i][r], dp[i-1][l-1] + Mx);
}
}
}
cout << dp[k][n] <<"\n";
return;
}
}
namespace Subtask2{
bool ckSub(){
return n <= N;
}
const int K = 105;
ll dp[K][N];
void process()
{
FOR(i, 0, k){
FOR(j, 0, n){
dp[i][j] = INF;
}
}
dp[0][0] = 0;
a[0] = 1e9;
FOR(curK, 1, k){
stack<int> st; st.push(0);
FOR(i, 1, n){
ll best = dp[curK-1][i-1] + a[i], best_pos = i;
// if a[i] >= a[st.top()], it can get optimal position from st.top()
while(!st.empty() && a[i] >= a[st.top()]){
if(ckmn(best, dp[curK-1][st.top()-1] + a[i])){
best_pos = st.top();
}
st.pop();
}
// a[i] < a[st.top()] then it dp can be = to st.top() or it can create a maximum len array from st.top() + 1 -> a[i]
if(st.top() > 0){
best = min(best, dp[curK][st.top()]);
best = min(best, dp[curK-1][st.top()] + a[i]);
}
dp[curK][i] = min(dp[curK][i], best);
st.push(best_pos), st.push(i);
}
}
cout << dp[k][n] <<"\n";
return;
}
}
void solve()
{
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
//Subtask1::process();
Subtask2::process();
return;
if(Subtask1::ckSub()){
return Subtask1::process(), void();
}
else{
return Subtask2::process(), void();
}
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
blocks.cpp: In function 'int main()':
blocks.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(name".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... |