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 "wiring.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define mp make_pair
#define pub push_back
#define pob pop_back()
#define ss second
#define ff first
#define mt make_tuple
#define pof pop_front()
#define fbo find_by_order
#define ook order_of_key
#define lb lower_bound
#define ub upper_bound
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
using pll = pair <ll, ll>;
using pii = pair <int, int>;
vector <ll> x, y;
ll dp[200002], z = -1, opt, k = 1, hd, st;
ll cost(vector <ll>& n, ll hed, vector <ll>& m ) {
ll cst = 0;
for (int i = hed; i < n.size() - 1; i++)
cst += n.back() - n[i];
for (int i = 1; i < m.size(); i++)
cst += m[i] - m[0];
cst += max((ll)n.size() - hed, (ll)m.size()) * (m[0] - n.back());
return cst;
}
ll calc(vector <ll>& n, vector <ll>& m) {
if ( dp[opt + hd] == 1e18 )
{
opt = 0;
st = cost(n, 0, m);
}
while ( opt > 0 && min(dp[opt + hd], dp[opt + hd + 1]) > min(dp[opt + hd], dp[opt + hd - 1]) + n.back() - n[opt - 1] + (max((ll)n.size() - opt + 1ll, (ll)m.size()) - max((ll)n.size() - opt, (ll)m.size())) * (m[0] - n.back()) )
st += n.back() - n[opt - 1] + (max((ll)n.size() - opt + 1ll, (ll)m.size()) - max((ll)n.size() - opt, (ll)m.size())) * (m[0] - n.back()), opt--;
return st + min(dp[hd + opt + 1], dp[hd + opt]);
}
long long min_total_length(vector<int> r, vector<int> b) {
int i = 0, j = 0;
for (int i1 = 1; i1 <= r.size() + b.size(); i1++)
dp[i1] = 1e18;
while ( i < r.size() || j < b.size() ) {
if ( i != r.size() && (j == b.size() || r[i] < b[j]) ) {
if ( z == -1 ) {
y.pub(r[i]);
} else {
if ( z == 1 )
{
hd += x.size();
x = y;
y.clear();
y.pub(r[i]);
if ( x.size() )
{
opt = x.size() - 1;
st = cost(x, opt, y);
dp[k] = calc(x, y);
}
} else {
y.pub(r[i]);
if ( x.size() )
{
st -= (max((ll)y.size() - 1ll, (ll)x.size() - opt) - max((ll)y.size(), (ll)x.size() - opt)) * (y[0] - x.back()) - y.back() + y[0];
dp[k] = calc(x, y);
}
}
}
i++;
z = 0;
} else {
if ( z == -1 ) {
y.pub(b[j]);
} else {
if ( z == 0 ) {
hd += x.size();
x = y;
y.clear();
y.pub(b[j]);
if ( x.size() )
{
opt = x.size() - 1;
st = cost(x, opt, y);
dp[k] = calc(x, y);
}
} else {
y.pub(b[j]);
if ( x.size() )
{
st -= (max((ll)y.size() - 1ll, (ll)x.size() - opt) - max((ll)y.size(), (ll)x.size() - opt)) * (y[0] - x.back()) - y.back() + y[0];
dp[k] = calc(x, y);
}
}
}
z = 1;
j++;
}
k++;
}
return dp[r.size() + b.size()];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int cost(std::vector<long long int>&, long long int, std::vector<long long int>&)':
wiring.cpp:31:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = hed; i < n.size() - 1; i++)
~~^~~~~~~~~~~~~~
wiring.cpp:34:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < m.size(); i++)
~~^~~~~~~~~~
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:59:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i1 = 1; i1 <= r.size() + b.size(); i1++)
~~~^~~~~~~~~~~~~~~~~~~~~~
wiring.cpp:62:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( i < r.size() || j < b.size() ) {
~~^~~~~~~~~~
wiring.cpp:62:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( i < r.size() || j < b.size() ) {
~~^~~~~~~~~~
wiring.cpp:64:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if ( i != r.size() && (j == b.size() || r[i] < b[j]) ) {
~~^~~~~~~~~~~
wiring.cpp:64:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if ( i != r.size() && (j == b.size() || r[i] < b[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... |