This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
inline void setin(string s) {
freopen(s.c_str(), "r", stdin);
}
inline void setout(string s) {
freopen(s.c_str(), "w", stdout);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 15;
int n, m;
pii a[N];
ll p[N];
vector <ll> dp[N];
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n = r.size(), m = b.size();
int sz = 0;
for(int i = 0, j = 0; i < r.size(); ++i) {
while(j < b.size() && b[j] < r[i])
a[++sz] = {2, b[j++]};
a[++sz] = {1, r[i]};
if(i + 1 == r.size()) {
for(; j < b.size(); ++j)
a[++sz] = {2, b[j]};
}
}
n = sz;
int g = 1;
dp[0].pb(0);
for(int i = 1; i <= n; ++i)
p[i] = p[i-1] + a[i].se;
for(int i = 1; i <= n; ++i, ++g) {
int j = i;
while(j + 1 <= n && a[j + 1].f == a[j].f)
++j;
int k = j + 1;
if(k == n + 1)
break;
while(k + 1 <= n && a[k + 1].f == a[k].f)
++k;
int len[2] = {j - i + 1, k - j};
if(g == 1)
dp[g].resize(len[0] + 2, inf);
dp[g + 1].resize(len[1] + 2, inf);
dp[g][len[0]] = dp[g - 1][0];
for(int l = len[0]; l; --l) {
Min(dp[g][l - 1], dp[g][l] + (a[j + 1].se - a[j - l + 1].se));
if(l <= len[1]) {
ll val = 1ll * l * a[j].se - (p[j] - p[j - l]);
val += (p[j + l] - p[j]) - 1ll * l * a[j + 1].se;
val += 1ll * l * (a[j + 1].se - a[j].se);
// for(int t = j - l + 1; t <= j; ++t)
// val += a[j].se - a[t].se;
// for(int t = j + 1; t <= j + l; ++t)
// val += a[t].se - a[j + 1].se;
// val += 1ll * l * (a[j + 1].se - a[j].se);
Min(dp[g + 1][len[1] - l], dp[g][l] + val);
}
}
for(int l = len[1]; l; --l) {
Min(dp[g + 1][l - 1], dp[g + 1][l] + (a[k - l + 1].se - a[j].se));
if(l <= len[0]) {
ll val = 1ll * a[j].se * l - (p[j] - p[j - l]);
val += (p[k] - p[k - l]) - 1ll * a[j + 1].se * l;
val += 1ll * l * (a[j + 1].se - a[j].se);
// for(int t = j - l + 1; t <= j; ++t)
// val += a[j].se - a[t].se;
// for(int t = k - l + 1; t <= k; ++t)
// val += a[t].se - a[j + 1].se;
// val += 1ll * l * (a[j + 1].se - a[j].se);
Min(dp[g + 1][0], dp[g + 1][l] + val);
}
}
i = j;
}
return dp[g][0];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:68:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0, j = 0; i < r.size(); ++i) {
~~^~~~~~~~~~
wiring.cpp:69:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(j < b.size() && b[j] < r[i])
~~^~~~~~~~~~
wiring.cpp:72:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i + 1 == r.size()) {
~~~~~~^~~~~~~~~~~
wiring.cpp:73:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; j < b.size(); ++j)
~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |