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;
//typedef long long ll;
//typedef pair<ll,ll> pll;
//typedef pair<pll,ll> plll;
//typedef pair<pll,pll> ppll;
//typedef long double ld;
//
//#define all(x) (x).begin(), (x).end()
//#define rall(x) (x).rbegin(), (x).rend()
//#define fst first
//#define snd second
//#define ins insert
//#define pb push_back
//template< typename T,typename V>ostream &operator<< (ostream &out,const pair<T,V> x){ out << "{" << x.fst << " : " << x.snd << "}"; return out;}template< typename T>ostream &operator<< (ostream &out,const set<T> x){ for(auto &it : x){ out << it << " "; } return out;}template< typename T>ostream &operator<< (ostream &out,const multiset<T> x){ for(auto &it : x){ out << it << " "; } return out;}template< typename T,typename V>ostream &operator<< (ostream &out,const map<T,V> x){ for(auto &it : x){ out << "[" << it.fst << "]" << " = " << it.snd << "\n"; } return out;}template< typename T>ostream &operator<< (ostream &out,const vector<T> x){ for(int i = 0;i < x.size() - 1; ++i){ out << x[i] << " "; } out << x.back(); return out;}template< typename T>ostream &operator<< (ostream &out,const vector<vector<T> > x){ for(int i = 0;i < x.size() - 1; ++i){ out << "[" << i << "]" << " = {" << x[i] << "}\n"; } out << "[" << x.size() - 1 << "]" << " = {" << x.back() << "}\n"; return out;}
//
//const ll N = 1e6 + 5;
//const ll MOD = 1e9 + 7;
//const ll INF = 1e17;
//vector<pll> a;
//ll dp[N];
//main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// //freopen(".in", "r", stdin);
// //freopen(".out", "w", stdout);
// ll n,m;
// cin >> n >> m;
// for(ll i = 0;i < n; ++i){
// ll x;
// cin >> x;
// a.pb({x,0});
// }
// for(ll i = 0;i < m; ++i){
// ll x;
// cin >> x;
// a.pb({x,1});
// }
// a.pb({-1,-1});
// dp[0] = 0;
// sort(all(a));
// ll mnb = INF;
// ll sumb = 0;
// ll cntb = 0;
// for(int i = 1;i <= a.size(); ++i){
// dp[i] = INF;
// if(i == 1 || a[i].snd != a[i - 1].snd){
// cntb = 1;
// mnb = a[i].fst;
// sumb = 0;
// }else{
// sumb += a[i].fst - mnb;
// cntb++;
// }
// ll cnta = 0,mxa = 0,suma = 0;
// for(int j = i - cntb;j > 0; --j){
// cnta++;
// mxa = max(mxa,a[j].fst);
// if(j < i - cntb && a[j].snd != a[j + 1].snd)break;
// suma += mxa - a[j].fst;
// dp[i] = min(dp[i],dp[j - 1] + sumb + suma + max(cnta,cntb) * (mnb - mxa));
// }
// }
// cout << dp[a.size() - 1];
// return 0;
//}
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> plll;
typedef pair<pll,pll> ppll;
typedef long double ld;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fst first
#define snd second
#define ins insert
#define pb push_back
template< typename T,typename V>ostream &operator<< (ostream &out,const pair<T,V> x){ out << "{" << x.fst << " : " << x.snd << "}"; return out;}template< typename T>ostream &operator<< (ostream &out,const set<T> x){ for(auto &it : x){ out << it << " "; } return out;}template< typename T>ostream &operator<< (ostream &out,const multiset<T> x){ for(auto &it : x){ out << it << " "; } return out;}template< typename T,typename V>ostream &operator<< (ostream &out,const map<T,V> x){ for(auto &it : x){ out << "[" << it.fst << "]" << " = " << it.snd << "\n"; } return out;}template< typename T>ostream &operator<< (ostream &out,const vector<T> x){ for(int i = 0;i < x.size() - 1; ++i){ out << x[i] << " "; } out << x.back(); return out;}template< typename T>ostream &operator<< (ostream &out,const vector<vector<T> > x){ for(int i = 0;i < x.size() - 1; ++i){ out << "[" << i << "]" << " = {" << x[i] << "}\n"; } out << "[" << x.size() - 1 << "]" << " = {" << x.back() << "}\n"; return out;}
const ll N = 1e6 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e17;
vector<pll> a;
ll dp[N];
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll n = r.size(),m = b.size();
for(ll i = 0;i < n; ++i){
ll x;
x = r[i];
a.pb({x,0});
}
for(ll i = 0;i < m; ++i){
ll x;
x = b[i];
a.pb({x,1});
}
a.pb({-1,-1});
dp[0] = 0;
sort(all(a));
ll mnb = INF;
ll sumb = 0;
ll cntb = 0;
for(int i = 1;i <= a.size(); ++i){
dp[i] = INF;
if(i == 1 || a[i].snd != a[i - 1].snd){
cntb = 1;
mnb = a[i].fst;
sumb = 0;
}else{
sumb += a[i].fst - mnb;
cntb++;
}
ll cnta = 0,mxa = 0,suma = 0;
for(int j = i - cntb;j > 0; --j){
cnta++;
mxa = max(mxa,a[j].fst);
if(j < i - cntb && a[j].snd != a[j + 1].snd)break;
suma += mxa - a[j].fst;
dp[i] = min(dp[i],dp[j - 1] + sumb + suma + max(cnta,cntb) * (mnb - mxa));
}
}
return dp[n + m];
}
/**
4 5
1 2 3 7
0 4 5 9 10
*/
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:106:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1;i <= a.size(); ++i){
~~^~~~~~~~~~~
# | 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... |