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[200001], z = -1, opt, k = 1, hd;
ll cost(vector <ll> n, int 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(n.size() - hed, m.size()) * (m[0] - n.back());
return cst;
}
ll calc(vector <ll> n, vector <ll> m) {
//
// cout <<"xxxxxxxxxxxx\n";
//
// for (auto p : n)
// cout << p << " ";
//
// cout << "\n";
//
// for (auto p : m)
// cout << p << " ";
// cout << "\n";
if ( dp[opt + hd] == 1e18 )
opt = 0;
while ( opt > 0 && dp[hd + opt] + cost(n, opt, m) > dp[hd + opt - 1] + cost(n, opt - 1, m) )
opt--;
// cout <<hd << " " << opt << "\n";
// cout << "xxxxxxxxxxxxxxxx\n";
return cost(n, opt, m) + 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;
opt = x.size() - 1;
y.clear();
y.pub(r[i]);
if ( x.size() )
dp[k] = calc(x, y);
} else {
y.pub(r[i]);
if ( x.size() )
dp[k] = calc(x, y);
}
}
i++;
z = 0;
} else {
if ( z == -1 ) {
z = 1;
y.pub(b[j]);
} else {
if ( z == 0 ) {
z = 1;
hd += x.size();
x = y;
opt = x.size() - 1;
y.clear();
y.pub(b[j]);
if ( x.size() )
dp[k] = calc(x, y);
} else {
y.pub(b[j]);
if ( x.size() )
dp[k] = calc(x, y);
}
}
j++;
}
// cout << dp[k] << "\n";
k++;
}
return dp[r.size() + b.size()];
}
/*
4 5
1 2 3 7
0 4 5 9 10
*/
Compilation message (stderr)
wiring.cpp: In function 'long long int cost(std::vector<long long int>, 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:67:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i1 = 1; i1 <= r.size() + b.size(); i1++)
~~~^~~~~~~~~~~~~~~~~~~~~~
wiring.cpp:70:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( i < r.size() || j < b.size() ) {
~~^~~~~~~~~~
wiring.cpp:70:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( i < r.size() || j < b.size() ) {
~~^~~~~~~~~~
wiring.cpp:71:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if ( i != r.size() && (j == b.size() || r[i] < b[j]) ) {
~~^~~~~~~~~~~
wiring.cpp:71: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... |