이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> v2i;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define rept(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
int search(vi& vec, int x) {
int a = 0, b = vec.size();
int mid = (a + b) / 2;
//cout << "Searching\n";
if (vec[vec.size() - 1] < x) {
//cout << "none found\n";
return -1;
}
if (vec[0] >= x) {
//cout << "first found\n";
return 0;
}
while (a <= b) {
if (vec[mid] >= x && vec[mid - 1] < x) {
//cout << mid << " found\n";
return mid;
}
else if (vec[mid] < x) {
//cout << mid << " too small\n";
a = mid + 1;
}
else {
//cout << mid << " too big\n";
b = mid - 1;
}
mid = (a + b) / 2;
}
//cout << a << " returned at end\n";
return a;
}
int searchsmaller(vi& vec, int x) {
int a = 0, b = vec.size();
int mid = (a + b) / 2;
if (vec[0] > x) return -1;
if (vec[vec.size() - 1] <= x) return vec.size() - 1;
while (a > b) {
if (vec[mid] <= x && vec[mid + 1] > x) return mid;
else if (vec[mid] <= x) {
a = mid + 1;
}
else {
b = mid - 1;
}
mid = (a + b) / 2;
}
return a;
}
//int search(vpi& vec, int x) {
// int a = 0, b = vec.size();
// int mid = (a + b) / 2;
// if (vec[vec.size() - 1].first < x) return -1;
// if (vec[0].first >= x) return 0;
// while (a >= b) {
// if (vec[mid].first == x) return mid;
// else if (vec[mid].first < x) {
// a = mid + 1;
// }
// else {
// b = mid - 1;
// }
// mid = (a + b) / 2;
// }
// return a;
//}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll length = 0;
int n = r.size(), m = b.size();
int ptr1 = 0, ptr2 = 0;
vpi allnodes; // red: 0, blue: 1
vpi r2, b2;
rep(i, m + n) {
if (ptr1 == r.size()) {
allnodes.pb({ b[ptr2], 1 });
b2.pb({ b[ptr2++], i });
}
else if (ptr2 == b.size()) {
allnodes.pb({ r[ptr1], 0 });
r2.pb({ b[ptr1++], i });
}
else if (r[ptr1] > b[ptr2]) {
allnodes.pb({ b[ptr2], 1 });
b2.pb({ b[ptr2++], i });
}
else {
allnodes.pb({ r[ptr1], 0 });
r2.pb({ b[ptr1++], i });
}
}
//cout << "Created allpoints array\n";
vb done(allnodes.size(), false);
rep(i, m + n) {
//cout << "Processing point at position " << allnodes[i].first << " " << done[i] << "\n";
if (!done[i]) {
int type = allnodes[i].second;
int c;
int next;
int pos;//debug
if (type == 1) {
//cout << "Searching for red point after " << allnodes[i].first << "\n";
c = search(r, allnodes[i].first);
while (c != -1 && done[r2[c].second]) {
//cout << "Searching for red point after " << r[c] + 1 << "\n";
c = search(r, r[c] + 1);
}
if (c == -1) {
//cout << "Searching for red point before " << allnodes[i].first << "\n";
c = searchsmaller(r, allnodes[i].first);
length += allnodes[i].first - r[c];
}
else {
length += r[c] - allnodes[i].first;
}
pos = r[c];
next = r2[c].second;
}
else {
//cout << "Searching for blue point after " << allnodes[i].first << "\n";
c = search(b, allnodes[i].first);
while (c != -1 && done[b2[c].second]) {
//cout << "Searching for blue point after " << b[c] + 1 << "\n";
c = search(b, b[c] + 1);
}
if (c == -1) {
//cout << "Searching for blue point before " << allnodes[i].first << "\n";
c = searchsmaller(b, allnodes[i].first);
length += allnodes[i].first - b[c];
}
else {
length += b[c] - allnodes[i].first;
}
pos = b[c];
next = b2[c].second;
}
done[i] = true;
done[next] = true;
//cout << "Connected point at position " << allnodes[i].first << " with point at position " << pos << "\n";
}
else {
//cout << "Point at position " << allnodes[i].first << " has already been processed\n";
}
}
return length;
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:96:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | if (ptr1 == r.size()) {
| ~~~~~^~~~~~~~~~~
wiring.cpp:100:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | else if (ptr2 == b.size()) {
| ~~~~~^~~~~~~~~~~
wiring.cpp:121:8: warning: variable 'pos' set but not used [-Wunused-but-set-variable]
121 | int pos;//debug
| ^~~
# | 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... |