Submission #978798

#TimeUsernameProblemLanguageResultExecution timeMemory
978798LOLOLOK blocks (IZhO14_blocks)C++17
100 / 100
848 ms19724 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; #define f first #define s second #define pb push_back #define ep emplace #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin()) #define mem(f,x) memset(f , x , sizeof(f)) #define sz(x) (int)(x).size() #define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b)) #define mxx *max_element #define mnn *min_element #define cntbit(x) __builtin_popcountll(x) #define len(x) (int)(x.length()) const int N = 1e5 + 10; ll f[N], a[N]; ll sp[N][20], n, k; int lg[N]; void build(int id) { for (int i = 0; i <= n; i++) { sp[i][0] = f[i]; } for (int j = 1; j < 20; j++) { for (int i = 0; i + (1 << j) <= n + 1; i++) { sp[i][j] = min(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]); } } } ll range(int l, int r, int id) { if (l > r) return 1e16; int t = lg[r - l + 1]; return min(sp[l][t], sp[r - (1 << t) + 1][t]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 2; i < N; i++) lg[i] = lg[i / 2] + 1; mem(f, 0x3f); cin >> n >> k; f[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int j = 1; j <= k; j++) { build(j - 1); stack <int> st; vector <ll> pr; for (int i = 0; i <= n; i++) { while (sz(st) && a[i] >= a[st.top()]) { st.pop(); pr.pop_back(); } int lst = sz(st) ? st.top() : 0; ll nxt = range(lst, i - 1, j - 1) + a[i]; if (sz(pr)) nxt = min(nxt, pr.back()); f[i] = nxt; st.push(i); ll v = sz(pr) ? pr.back() : 1e18; pr.pb(min(v, range(lst, i - 1, j - 1) + a[i])); } } cout << f[n] << '\n'; } // 2 2 // 1 2
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...