이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
long long min_total_length(std::vector<int> red, std::vector<int> blue) {
long long n = red.size(), m = blue.size();
vector<array<long long, 2> > wirings;
wirings.push_back({0, 0});
for(long long i = 0; i < n; i++) {
wirings.push_back({red[i], 0});
}
for(long long i = 0; i < m; i++) {
wirings.push_back({blue[i], 1});
}
sort(wirings.begin() + 1, wirings.end());
long long total = wirings.size();
long long closest[total + 1];
for(long long i = 0; i <= total; i++) {
closest[i] = 1e9;
}
long long lastPoint[2] = {-1, -1};
for(long long i = 1; i < wirings.size(); i++) {
//cout << "Current color = " << ((wirings[i][1]) ? "blue, " : "red, ");
if(lastPoint[1 - wirings[i][1]] != -1) {
closest[i] = wirings[i][0] - wirings[lastPoint[1 - wirings[i][1]]][0];
}
//cout << "Last index of the other color = " << lastPoint[1 - wirings[i][1]] << "\n";
lastPoint[wirings[i][1]] = i;
}
lastPoint[0] = -1;
lastPoint[1] = -1;
//cout << "Going in reverse\n";
for(long long i = wirings.size() - 1; i > 0; i--) {
//cout << "Current color = " << ((wirings[i][1]) ? "blue, " : "red, ");
if(lastPoint[1 - wirings[i][1]] != -1) {
closest[i] = min(closest[i], wirings[lastPoint[1 - wirings[i][1]]][0] - wirings[i][0]);
}
//cout << "Last index of the other color = " << lastPoint[1 - wirings[i][1]] << "\n";
lastPoint[wirings[i][1]] = i;
}
/*cout << "Outputting all the points, along with their colors:\n";
for(int i = 1; i < wirings.size(); i++) {
cout << wirings[i][0] << " " << ((wirings[i][1]) ? "blue" : "red") << "\n";
}*/
/*cout << "Outputting the closest array\n";
for(int i = 1; i < total; i++) {
cout << closest[i] << " ";
}
cout << "\n";*/
map<long long, long long> lastSeen;
long long pref[total + 1], prev[total + 1];
pref[0] = 0;
long long cur_abs_value = 0;
for(long long i = 1; i < wirings.size(); i++) {
//cout << "i = " << i << ", cur_abs_value before: " << cur_abs_value << ", ";
if(wirings[i][1] == 0) {
cur_abs_value++;
pref[i] = pref[i - 1] + wirings[i][0];
}
else {
cur_abs_value--;
pref[i] = pref[i - 1] - wirings[i][0];
}
if(!lastSeen.count(cur_abs_value)) {
prev[i] = -1;
}
else {
prev[i] = lastSeen[cur_abs_value];
}
lastSeen[cur_abs_value] = i;
//cout << "cur_abs_value after: " << cur_abs_value << "\n";
}
long long dp[total + 1];
for(long long i = 0; i <= total; i++) {
dp[i] = 0;
}
for(long long i = 1; i < wirings.size(); i++) {
dp[i] = dp[i - 1] + closest[i];
if(prev[i] != -1) {
dp[i] = min(dp[i], dp[prev[i]] + abs(pref[i] - pref[prev[i]]));
}
}
/*for(long long i = 1; i < wirings.size(); i++) {
cout << dp[i] << " ";
}
cout << "\n";*/
return dp[total - 1] - dp[1];
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:22:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(long long i = 1; i < wirings.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
wiring.cpp:54:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(long long i = 1; i < wirings.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
wiring.cpp:77:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(long long i = 1; i < wirings.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... |