이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "railroad.h"
#define ll long long
#define vt vector
#define pb push_back
#define ar array
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()
using namespace std;
/*
1. simplify
2. add new elements
3. brute force solution
4. optimize
5. start implementing
*/
// --- templates ---
// --- code ---
vt<int> p;
int find(int u){
if(p[u] == u){
return u;
}
return p[u] = find(p[u]);
}
bool join(int u, int v){
if((u = find(u)) == (v = find(v))){
return false;
}
p[u] = v;
return true;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = sz(s);
vt<int> comp = s;
comp.insert(comp.end(), all(t));
sort(all(comp));
vt<int> a(sz(comp));
p.resize(sz(comp));
iota(all(p), 0);
for(int i = 0; i < n; i++){
s[i] = lower_bound(all(comp), s[i]) - comp.begin();
t[i] = lower_bound(all(comp), t[i]) - comp.begin();
a[s[i]]++;
a[t[i]]--;
join(s[i], t[i]);
}
ll ans = 0;
vt<ar<int, 2>> e;
for(int i = 0; i < sz(comp) - 1; i++){
a[i + 1] += a[i];
ans += max((ll)0, ((ll) (a[i] - 1)) * (comp[i + 1] - comp[i]));
if(a[i] != 1){
join(i, i + 1);
}
e.pb({comp[i + 1] - comp[i], i});
}
sort(all(e));
for(int i = 0; i < sz(e); i++){
if(join(e[i][1], e[i][1] + 1)){
ans += e[i][0];
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < sz(comp) - 1; i++){
| ~~^~~~~~~~~~~~~~
railroad.cpp:72:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < sz(e); 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... |