This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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){
if(x == rhs.x) return col < rhs.col;
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 void find(int i){
int r = last[i][v[i].col ^ 1];
int l = last[r][v[i].col] + 1;
// cout<<l<<r<<"\n";
long long d = v[r + 1].x - v[r].x;
ans[i] = 400000000000000000;
for(; l <= r; l++){
ans[i] = min(ans[i], ans[l - 1] + sum(l, r, r) + sum(r + 1, i, r + 1) + d * max(r - l + 1, i - r));
}
}
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;
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;
}
int t = i;
for(; i < v.size(); i++){
if(v[i].col != v[t].col) break;
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;
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++){
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;
find(i);
// cout<<ans[i]<<"\n";
}
return ans[v.size() - 1];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:58:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; i < v.size(); i++){
~~^~~~~~~~~~
wiring.cpp:66:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; i < v.size(); i++){
~~^~~~~~~~~~
wiring.cpp:24: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:25: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... |