제출 #1142426

#제출 시각아이디문제언어결과실행 시간메모리
1142426g4yuhgK blocks (IZhO14_blocks)C++20
100 / 100
308 ms81160 KiB
//Huyduocdithitp #include <bits/stdc++.h> typedef long long ll; #define endl '\n' #define yes cout<<"YES"<<endl; #define no cout<<"NO"<<endl; #define fi first #define se second #define pii pair<ll, ll> #define inf 1e18 #define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define MP make_pair #define TASK "ghuy4g" #define start if(fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);} #define LOG 19 #define N 100005 using namespace std; ll n, k; ll a[N], f[N][101]; void sub2() { ll ln = 0; for (int i = 1; i <= n; i ++) { ln = max(ln, a[i]); f[i][1] = ln; } for (int j = 2; j <= k; j ++) { stack<pii> st; stack<ll> stmin; st.push({f[j - 1][j - 1], a[j]}); stmin.push(st.top().fi + st.top().se); ll mini = st.top().fi + st.top().se; for (int i = j; i <= n; i ++) { f[i][j] = mini; mini = inf; pii moi = {f[i][j - 1], a[i + 1]}; while (st.size() && st.top().se <= a[i + 1]) { moi.fi = min(moi.fi, st.top().fi); st.pop(); stmin.pop(); } if (st.size() && st.top().se > a[i + 1]) { mini = min(mini, st.top().fi + st.top().se); } if (stmin.size()) { stmin.push(min(moi.fi + moi.se, stmin.top())); } else { stmin.push(moi.fi + moi.se); } st.push(moi); mini = min(mini, moi.fi + moi.se); if (stmin.size()) mini = min(mini, stmin.top()); } } cout << f[n][k]; } void sub1() { memset(f, 60, sizeof(f)); ll ln = 0; for (int i = 1; i <= n; i ++) { ln = max(ln, a[i]); f[i][1] = ln; } for (int j = 2; j <= k; j ++) { for (int i = j; i <= n; i ++) { ll maxi = a[i]; for (int z = i; z >= j; z --) { maxi = max(maxi, a[z]); f[i][j] = min(f[i][j], maxi + f[z - 1][j - 1]); if (j == 2 && i == n && maxi + f[z - 1][j - 1] == 5) { //cout << z - 1 << " " << j - 1 << " " << maxi << endl; } } } } cout << f[n][k]; } signed main(void) { faster; cin >> n >> k; for (int i = 1; i <= n; i ++) { cin >> a[i]; } sub2(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...