Submission #640187

#TimeUsernameProblemLanguageResultExecution timeMemory
640187danikoynovPeru (RMI20_peru)C++14
100 / 100
468 ms98072 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2.5e6 + 10; const ll inf = 1e18, mod = 1e9 + 7; ll a[maxn], dp[maxn], to[maxn]; int solve(int n, int k, int *S) { for (int i = 0; i < n; i ++) a[i + 1] = S[i]; stack < int > sc; for (int i = 1; i <= n; i ++) { while(!sc.empty() && a[sc.top()] < a[i]) { to[sc.top()] = i; sc.pop(); } sc.push(i); } deque < int > dq; multiset < ll > mt; for (int i = 1; i <= n; i ++) { while(!dq.empty() && a[dq.back()] <= a[i]) { ll sum = a[dq.back()]; dq.pop_back(); if (!dq.empty()) { sum = sum + dp[dq.back()]; mt.erase(mt.find(sum)); } } if (!dq.empty()) { ll sum = dp[dq.back()] + a[i]; mt.insert(sum); } dq.push_back(i); while(!dq.empty() && dq.front() <= i - k) { ll sum = dp[dq.front()]; dq.pop_front(); if (!dq.empty()) { sum = sum + a[dq.front()]; mt.erase(mt.find(sum)); } } dp[i] = inf; if (!mt.empty()) dp[i] = *mt.begin(); dp[i] = min(dp[i], dp[max(0, i - k)] + a[dq.front()]); } /**for (int i = 1; i <= n; i ++) cout << dp[i] << " "; cout << endl;*/ ll cur = 1, ans = 0; for (int i = n; i > 0; i --) { ans = (ans + (dp[i] % mod) * cur) % mod; cur = (cur * (ll)23) % mod; } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...