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 <iostream>
#define MAX 1000000000000000000
using namespace std;
typedef long long ll;
ll abs(ll x) {
return x>=0 ? x:-x;
}
bool isSub1(int rSize, int bSize) {
return rSize<=200 && bSize<=200;
}
ll sub1Dp[210][210]; // sub1Dp[i][j]: minCost when connecting r[0~i], b[0~j]
ll sub1(vector<int> &r, vector<int> &b) {
int n = r.size(), m = b.size();
sub1Dp[0][0] = abs(r[0] - b[0]);
for(int j=1; j<m; j++) {
sub1Dp[0][j] = sub1Dp[0][j-1] + abs(r[0] - b[j]);
}
for(int i=1; i<n; i++) {
sub1Dp[i][0] = sub1Dp[i-1][0] + abs(r[i] - b[0]);
for(int j=1; j<m; j++) {
// r[i], b[j] should be connected
ll connectingCost = abs(r[i] - b[j]);
// case 1: r[i], b[j] are seperated to the rest
ll t = sub1Dp[i-1][j-1]; // t is the restCost
// case 2: r[i] is connected to the rest
// in this case, b[j] is seperated to the rest
t = min(t, sub1Dp[i][j-1]);
// case 3: b[j] is connected to the rest
// in this case, r[i] is seperated to the rest
t = min(t, sub1Dp[i-1][j]);
sub1Dp[i][j] = t + connectingCost;
}
}
return sub1Dp[n-1][m-1];
}
bool isSub2(vector<int> &r, vector<int> &b) {
return r.back() < b[0];
}
ll sub2(vector<int> &left, vector<int> &right, int lSt, int lEd, int rSt, int rEd) {
ll lSum = 0;
for(int i=lSt; i<=lEd; i++) {
lSum += left[lEd] - left[i];
}
ll rSum = 0;
for(int j=rSt; j<=rEd; j++) {
rSum += right[j] - right[rSt];
}
ll connectingCost = (ll)(right[rSt] - left[lEd]) * max(lEd-lSt+1, rEd-rSt+1);
return lSum + rSum + connectingCost;
}
bool isSub3(vector<int> &r, vector<int> &b) {
int n = r.size(), m = b.size();
vector<int> v;
int i=0, j=0;
while(i < n || j < m) {
if(i == n) { v.push_back(0); j++;}
else if(j == m) { v.push_back(1); i++;}
else if(r[i] < b[j]) { v.push_back(1); i++;}
else { v.push_back(0); j++;}
}
int cnt = 1;
for(int i=1; i<v.size(); i++) {
if(v[i] == v[i-1]) cnt++;
else cnt = 1;
if(cnt == 7) return false;
}
return true;
}
void makeVectors(vector<int> &color, vector<int> &idx, vector<int> &r, vector<int> &b) {
int n = r.size(), m = b.size();
int i=0, j=0;
while(i < n || j < m) {
if(i == n) {color.push_back(0); idx.push_back(j); j++;}
else if(j == m) {color.push_back(1); idx.push_back(i); i++;}
else if(r[i] < b[j]) {color.push_back(1); idx.push_back(i); i++;}
else {color.push_back(0); idx.push_back(j); j++;}
}
}
void printVector(vector<int> &v) {
printf("{");
for(int i=0; i<v.size(); i++) {
printf("%d,", v[i]);
}
printf("}\n");
}
ll sub3Dp[200010]; // minCost of v[0~i]
ll sub3(vector<int> &r, vector<int> &b) {
vector<int> color, rbIdx;
makeVectors(color, rbIdx, r, b);
int vSize = color.size();
int vSt = -1;
for(int vIdx=0; vIdx<vSize-1; vIdx++) {
sub3Dp[vIdx] = MAX;
if(color[vIdx] != color[vIdx+1]) {
vSt = vIdx + 1;
break;
}
}
int vPrevL = -1, vCurrL = -1;
for(int vIdx=vSt; vIdx<vSize; vIdx++) {
sub3Dp[vIdx] = MAX;
if(color[vIdx] != color[vIdx-1]) {
vPrevL = vCurrL;
vCurrL = vIdx;
}
for(int i=vCurrL-1; i>=vPrevL; i--) {
bool reversed = color[vIdx] == 1;
ll cost;
if(reversed) cost = sub2(b, r, rbIdx[i], rbIdx[vCurrL-1], rbIdx[vCurrL], rbIdx[vIdx]);
else cost = sub2(r, b, rbIdx[i], rbIdx[vCurrL-1], rbIdx[vCurrL], rbIdx[vIdx]);
ll rest;
if(vPrevL == 0) {
if(i > vPrevL) continue;
else { // i == vPrevL (=0)
sub3Dp[vIdx] = cost;
}
}
else {
rest = sub3Dp[i-1];
if(i == vPrevL) rest = min(rest, sub3Dp[i]);
sub3Dp[vIdx] = min(sub3Dp[vIdx], cost + rest);
}
}
//printf("sub3Dp[%d] = %lld\n", vIdx, sub3Dp[vIdx]);
}
return sub3Dp[vSize-1];
}
ll min_total_length(vector<int> r, vector<int> b) {
int n = r.size(), m = b.size();
if(isSub1(n, m)) {
return sub1(r, b);
}
else if(isSub2(r, b)) {
return sub2(r, b, 0, n-1, 0, m-1);
}
else if(isSub3(r, b)) {
return sub3(r, b);
}
return -1;
}
Compilation message (stderr)
wiring.cpp: In function 'bool isSub3(std::vector<int>&, std::vector<int>&)':
wiring.cpp:84:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=1; i<v.size(); i++) {
| ~^~~~~~~~~
wiring.cpp: In function 'void printVector(std::vector<int>&)':
wiring.cpp:108:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i=0; i<v.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... |