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>
using namespace std;
#define int long long
int n, m, ans;
vector<int> fullList;
unordered_map<int, bool> isRed, isBlue;
vector<vector<int>> segments;
vector<vector<int>> prefixR;
vector<vector<int>> prefixL;
int calcWire(int s, int l, int r) {
int res = 0;
res += prefixL[s][l];
res += (segments[s].size()-l) * (segments[s+1][0]-segments[s].back());
res += prefixR[s+1][r];
res += (r+1) * (segments[s+1][0]-segments[s].back());
res -= min((int)segments[s].size()-l, r+1)*(segments[s+1][0]-segments[s].back());
return res;
}
int dp[205][205];
long long min_total_length(vector<signed> r, vector<signed> b) {
n = r.size();
m = b.size();
fullList.resize(n+m);
merge(r.begin(), r.end(), b.begin(), b.end(), fullList.begin());
for(auto x: r)
isRed[x]=1;
for(auto x: b)
isBlue[x]=1;
for(int i = 0; i < m+n; i++) {
if(i==0 || isRed[fullList[i]] != isRed[fullList[i-1]]) {
segments.push_back(vector<int>(0));
prefixL.push_back(vector<int>(0));
prefixR.push_back(vector<int>(0));
segments.back().push_back(fullList[i]);
} else {
segments.back().push_back(fullList[i]);
}
}
for(int s = 0; s < segments.size(); s++) {
prefixR[s].push_back(0);
for(int i = 1; i < segments[s].size(); i++)
prefixR[s].push_back(prefixR[s][i-1]+segments[s][i]-segments[s][0]);
reverse(segments[s].begin(), segments[s].end());
prefixL[s].push_back(0);
for(int i = 1; i < segments[s].size(); i++)
prefixL[s].push_back(prefixL[s][i-1]+segments[s][0]-segments[s][i]);
reverse(prefixL[s].begin(), prefixL[s].end());
reverse(segments[s].begin(), segments[s].end());
}
memset(dp, 63, sizeof(dp));
for(int j = 0; j < segments[1].size(); j++)
dp[1][j] = calcWire(0, 0, j);
// for(int s = 2; s < segments.size(); s++) {
// for(int i = 0; i < segments[s].size(); i++) {
// dp[s][i] = min(dp[s][i], dp[s-2][segments[s-2].size()-1] + calcWire(s-1, 0, i));
// for(int j = 1; j < segments[s-1].size(); j++)
// dp[s][i] = min(dp[s][i], dp[s-1][j-1] + calcWire(s-1, j, i));
// }
// }
return dp[segments.size()-1][segments.back().size()-1];
if(segments.size() == 2)
return calcWire(0, 0, m-1);
return -1;
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:46:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int s = 0; s < segments.size(); s++) {
~~^~~~~~~~~~~~~~~~~
wiring.cpp:48:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < segments[s].size(); i++)
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp:52:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < segments[s].size(); i++)
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp:59:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < segments[1].size(); 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... |