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 <bits/stdc++.h>
using namespace std;
#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define F first
#define S second
#define pb push_back
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define all(x) x.begin(), x.end()
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
typedef vector<pl> vpl;
vector<unordered_map<ll, ll>> memo;
ll n;
vpl v;
ll dp(ll pos, ll am, ll red = 1, ll changed = 0){
if(am>n) return 1e18;
if(pos == n) return (am?1e18:0);
if(memo[pos].count(red+changed*2+am*4)) return memo[pos][red+changed*2+am*4];
ll &ans = memo[pos][red+changed*2+am*4];
// deb2(pos, am);
ans = 1e18;
if(changed) ans = dp(pos+1, am, red)+(v[pos+1].F-v[pos].F)*am;
if(am==0) ans = min(dp(pos, am+1, v[pos].S, 1), ans);
else if(v[pos].S == red) ans = min(dp(pos, am+1, red, 1), ans);
else ans = min(dp(pos, am-1, red, 1), ans);
return ans;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
fo(i, r.size()){
v.pb({r[i], 1});
}
fo(i, b.size()){
v.pb({b[i], 0});
}
sort(all(v));
n = v.size();
v.pb(v[n-1]);
memo.assign(n+1, {});
return dp(0, 0);
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:5:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define fo(i, n) for(ll i = 0; i<(n); i++)
| ^
wiring.cpp:38:5: note: in expansion of macro 'fo'
38 | fo(i, r.size()){
| ^~
wiring.cpp:5:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define fo(i, n) for(ll i = 0; i<(n); i++)
| ^
wiring.cpp:41:5: note: in expansion of macro 'fo'
41 | fo(i, b.size()){
| ^~
# | 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... |