Submission #1142421

#TimeUsernameProblemLanguageResultExecution timeMemory
1142421g4yuhgK blocks (IZhO14_blocks)C++20
53 / 100
1097 ms79440 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; st.push({f[j - 1][j - 1], a[j]}); 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(); } if (st.size() && st.top().se > a[i + 1]) { mini = min(mini, st.top().fi + st.top().se); } st.push(moi); mini = min(mini, moi.fi + moi.se); } } 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]; } sub1(); 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...