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> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <ll, ll>
#define sz(x) (int)((x).size())
#include "wiring.h"
const int N = 2e3 + 11;
const ll INF64 = 3e18 + 1;
ll dp[N][N];
ll min_total_length(vector<int> r, vector<int> b){
vector<ll>red, blue;
red.push_back(0);
blue.push_back(0);
for(auto it : r)red.push_back((ll)it);
for(auto it : b)blue.push_back((ll)it);
if(red[sz(r)] <= blue[1]){
ll ans = 0;
for(int i = 1; i < sz(red); i++){
ans += (red[sz(red) - 1] - red[i]);
}
for(int i = 1; i < sz(blue); i++){
ans += (blue[i] - blue[1]);
}
ans += max(sz(r), sz(b))*(blue[1] - red[sz(r)]);
return ans;
}
if(sz(r) <= 200 && sz(b) <= 200){
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for(int i = 1; i < sz(red); i++){
for(int j = 1; j < sz(blue); j++){
dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + abs(red[i] - blue[j]);
}
}
return dp[sz(red) - 1][sz(blue) - 1];
}
ll ans = 0;
vector<pi>v;
v.push_back({-1, -1});
for(auto it : r){
v.push_back({it, 0});
}
for(auto it : b){
v.push_back({it, 1});
}
sort(all(v));
for(int l = 1; l < sz(v); l += 7){
vector<ll>R, B;
R.pb(0);
B.pb(0);
for(int i = l; i < min(l + 7, sz(v)); i++){
if(v[i].ss == 0){
R.push_back(v[i].ff);
}else B.push_back(v[i].ff);
}
for(int i = 1; i < sz(R); i++){
for(int j = 1; j < sz(B); j++){
dp[i][j] = INF64;
}
}
dp[0][0] = 0;
for(int i = 1; i < sz(R); i++){
for(int j = 1; j < sz(B); j++){
dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + abs(R[i] - B[j]);
}
}
ans += dp[sz(R) - 1][sz(B) - 1];
}
return ans;
}
# | 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... |