Promotion: 5% discount for almost all products! Another 5% discount for cart amount greater than 500usd! Contact us for more discount if large-value purchase! Dismiss
Slope trick code (C++):
[ dp[i] = \min_j < i (dp[j] + |a_i - a_j| \cdot C + \textconstant) ]
for (int i = 1; i <= n; i++) to function left.push(x); right.push(x); long long lmax = left.top(); long long rmin = right.top(); if (lmax > rmin) ans += lmax - rmin; left.pop(); right.pop(); left.push(rmin); right.push(lmax);
priority_queue<long long> left; // max-heap priority_queue<long long, vector<long long>, greater<long long>> right; // min-heap long long ans = 0;
#include <bits/stdc++.h> using namespace std; int main() int n; cin >> n;
Slope Hack !!hot!! Now
Slope trick code (C++):
[ dp[i] = \min_j < i (dp[j] + |a_i - a_j| \cdot C + \textconstant) ]
for (int i = 1; i <= n; i++) to function left.push(x); right.push(x); long long lmax = left.top(); long long rmin = right.top(); if (lmax > rmin) ans += lmax - rmin; left.pop(); right.pop(); left.push(rmin); right.push(lmax);
priority_queue<long long> left; // max-heap priority_queue<long long, vector<long long>, greater<long long>> right; // min-heap long long ans = 0;
#include <bits/stdc++.h> using namespace std; int main() int n; cin >> n;