이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
#define INF 1000000001LL
using namespace std;
struct Node{
long long x;
bool col;
Node(long long _x, bool _col){
x = _x;
col = _col;
}
bool operator<(const Node& rhs){
return x < rhs.x;
}
};
vector<Node> v;
int last[200005][2];
long long ans[200005], s[200005];
inline long long sum(int l, int r, int x){
if(l == x) return s[r] - s[l - 1] - v[l].x * (r - l + 1);
return v[r].x * (r - l + 1) - s[r] + s[l - 1];
}
inline long long func(int i, int l, int r){
return ans[l - 1] + sum(l, r, r) + sum(r + 1, i, r + 1) + (v[r + 1].x - v[r].x) * max(r - l + 1, i - r);
}
inline void find(int i){
int R = last[i][v[i].col ^ 1];
int r = R ;
int l = last[R][v[i].col] + 1;
int m;
ans[i] = ans[i - 1] + v[i].x - v[r].x;
ans[i] = min(ans[i], func(i, l, R));
if(l == r){
return;
}
while(l + 1!= r){
m = (l + r) >> 1;
if(func(i, m, R) < func(i, m + 1, R)) r = m;
else l = m;
}
ans[i] = min(ans[i], func(i, r, R));
}
inline void basic(int i){
last[i][0] = last[i - 1][0];
last[i][1] = last[i - 1][1];
last[i][v[i].col] = i;
s[i] = s[i - 1] + v[i].x;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
for(auto x : r){
v.push_back(Node(x, 0));
}
for(auto x : b){
v.push_back(Node(x, 1));
}
sort(v.begin(), v.end());
s[0] = v[0].x;
int i;
for(i = 1; i > 0; i++){
if(v[i].col != v[i - 1].col) break;
basic(i);
}
int t = i;
ans[t - 1] = 40000000000000000;
for(; i < v.size(); i++){
if(v[i].col != v[t].col) break;
basic(i);
ans[i] = sum(0, t - 1, t - 1) + sum(t, i, t) + (v[t].x - v[t - 1].x) * max(t, i - t + 1);
}
for(; i < v.size(); i++){
basic(i);
find(i);
}
return ans[v.size() - 1];
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:71:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; i < v.size(); i++){
~~^~~~~~~~~~
wiring.cpp:76:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; i < v.size(); i++){
~~^~~~~~~~~~
wiring.cpp:23:34: warning: array subscript is below array bounds [-Warray-bounds]
if(l == x) return s[r] - s[l - 1] - v[l].x * (r - l + 1);
~~~~~~~^
wiring.cpp:24:46: warning: array subscript is below array bounds [-Warray-bounds]
return v[r].x * (r - l + 1) - s[r] + s[l - 1];
~~~~~~~^
# | 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... |