Submission #641407

#TimeUsernameProblemLanguageResultExecution timeMemory
641407AlexandruabcdePeru (RMI20_peru)C++14
100 / 100
553 ms78532 KiB
#include "peru.h" #include <iostream> #include <deque> #include <queue> #include <vector> using namespace std; constexpr int NMAX = 2500005; constexpr int MOD = 1e9 + 7; typedef long long LL; constexpr LL INF = 1LL * 1e16; typedef pair <LL, int> PLI; int ans = 0; LL dp[NMAX]; LL val[NMAX]; int solve(int n, int k, int* v){ deque <int> D; priority_queue <PLI, vector<PLI>, greater<PLI> > H; for (int i = 0; i < n; ++ i ) { dp[i] = INF; while (!D.empty() && v[D.back()] <= v[i]) { val[D.back()] = -1; D.pop_back(); } while (!D.empty() && D.front() <= i - k) { val[D.front()] = -1; D.pop_front(); } D.push_back(i); if (!D.empty()) { val[D.front()] = 1LL * v[D.front()] + (i - k < 0 ? 0 : dp[i - k]); H.push({val[D.front()], D.front()}); } if (D.size() >= 2) { val[D.back()] = 1LL * v[D.back()] + dp[D[D.size()-2]]; H.push({val[D.back()], D.back()}); } else { val[D.back()] = 1LL * v[D.back()] + (i - k < 0 ? 0 : dp[i - k]); H.push({val[D.back()], D.back()}); } while (!H.empty() && val[H.top().second] != H.top().first) H.pop(); if (!H.empty()) dp[i] = H.top().first; else dp[i] = 1LL * v[D.front()] + (i - k < 0 ? 0 : dp[i - k]); } for (int i = 0; i < n; ++ i ) ans = (23LL * ans + dp[i] % MOD) % MOD; return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...