Submission #442149

#TimeUsernameProblemLanguageResultExecution timeMemory
442149Vladth11Peru (RMI20_peru)C++14
100 / 100
222 ms90084 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define debug(x) cerr << #x << " " << x << "\n" #define debugs(x) cerr << #x << " " << x << " " #include "peru.h" using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef pair <ll, ll> pii; typedef pair <long double, pii> muchie; typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST; const ll NMAX = 2500002; const ll INF = (1LL << 60); const ll HALF = (1LL << 59); const ll MOD = 1000000007; const ll BLOCK = 318; const ll base = 31; const ll nr_of_bits = 21; const ll LIMIT = 1000; ll dp[NMAX]; ll a[NMAX]; ll put[NMAX]; ll r[NMAX]; class MyDeque{ queue <ll> q; ll stiva[NMAX]; ll vf; ll minim[NMAX]; deque <pii> d; public: void insertDeque(pii x){ while(d.size() && d.back().first >= x.first){ d.pop_back(); } d.push_back(x); } void eraseDeque(ll x){ if(d.size() && d.front().second == x) d.pop_front(); } void update(ll lower_limit){ while(q.size() && q.front() < lower_limit){ eraseDeque(q.front()); q.pop(); } } void insertL(ll x){ ll sum = -1; if(q.size()){ sum = a[x] + dp[q.back()]; } if(sum != -1) insertDeque({sum, q.back()}); vf = 0; q.push(x); } void insertR(ll x){ ll sum = -1; while(vf && a[stiva[vf]] <= a[x]){ vf--; } if(vf){ sum = a[x] + dp[stiva[vf]]; } stiva[++vf] = x; minim[0] = INF; if(sum != -1) minim[vf] = min(minim[vf - 1], sum); else minim[vf] = INF; } void insert(ll x){ ///pot verifica chestii aici if(r[x]){ insertR(x); }else{ insertL(x); } } ll size(){ return q.size() + vf; } ll front(){ if(q.size()) return q.front(); return stiva[1]; } ll best(){ ll sol = INF; if(vf) sol = min(sol, minim[vf]); if(d.size()) sol = min(sol, d.front().first); if(q.size() && vf){ sol = min(sol, dp[q.back()] + a[stiva[1]]); } return sol; ///mai am de adaugat } }dq; int solve(int n, int k, int* v) { ll sol = 0; for(ll i = 0; i < n; i++) a[i + 1] = v[i]; put[0] = 1; for(ll i = 1; i <= n; i++) { put[i] = put[i - 1] * 23; put[i] %= MOD; } deque <ll> sim; for(ll i = 1; i <= n; i++){ while(sim.size() && sim.front() < i - k + 1) sim.pop_front(); while(sim.size() && a[sim.back()] <= a[i]){ r[sim.back()] = 1; sim.pop_back(); } sim.push_back(i); } for(ll i = 1; i <= n; i++) { dq.update(i - k + 1); dq.insert(i); dp[i] = INF; if(dq.size()) dp[i] = dp[max(0LL, i - k)] + a[dq.front()]; /// i - k il prelucram separat dp[i] = min(dp[i], dq.best()); // debug(dp[i]); sol += ((dp[i] % MOD) * put[n - i]) % MOD; sol %= MOD; } return sol; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...