이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <algorithm>
#include <utility>
using namespace std;
vector<pair<int, int> > locs;
int uf[400005];
int pre[400005];
int fin(int x)
{
if (uf[x] == x) return x;
return uf[x] = fin(uf[x]);
}
void un(int x, int y)
{
x = fin(x); y = fin(y);
if (x != y) uf[x] = y;
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
int n = (int)s.size();
for (int i = 0; i < 2 * n; i++) {
uf[i] = i; pre[i] = -1;
}
for (int i = 0; i < n; i++) locs.push_back(make_pair(s[i], i + 1));
for (int i = 0; i < n; i++) locs.push_back(make_pair(t[i], -i - 1));
sort(locs.begin(), locs.end());
for (int i = 0; i < 2 * n; i++) {
int idx;
if (locs[i].second < 0) {
idx = -locs[i].second - 1;
} else {
idx = locs[i].second - 1;
}
//printf("! %d %d\n", locs[i].first, idx);
if (pre[idx] != -1) {
un(i, pre[idx]);
//printf("! %d %d\n", i, pre[idx]);
} else {
pre[idx] = i;
}
}
int rightcnt = 1;
vector<pair<int, int> > edges;
long long ans = 0;
for (int i = 0; i < 2 * n; i++) {
if (locs[i].second < 0) {
rightcnt++;
} else {
rightcnt--;
}
if (!rightcnt) {
edges.push_back(
make_pair(locs[i+1].first - locs[i].first, i));
} else if (i != 2 * n - 1) {
un(i, i+1);
//printf("add %d %d\n", i, rightcnt);
if (rightcnt < 0) ans += (long long)(-rightcnt) *
(long long)(locs[i+1].first - locs[i].first);
}
}
sort(edges.begin(), edges.end());
for (int i = 0; i < edges.size(); i++) {
if (fin(edges[i].second) != fin(edges[i].second + 1)) {
ans += (long long)edges[i].first;
un(edges[i].second, edges[i].second + 1);
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < edges.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... |