이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "wiring.h"
using namespace std;
const int MAX_N = 100000;
const long long INF = (1LL<<30) * MAX_N;
struct Range {
int l, r, color;
Range(int _l, int _r, int _color):l(_l), r(_r), color(_color) {}
};
vector<pair<int, int> > node;
vector<Range> ranges;
long long _dp[1+2*MAX_N], *dp = _dp + 1;
long long _sum[1+2*MAX_N], *sum = _sum + 1;
void initAll(int n) {
for(int i = -1; i < n; ++i)
dp[i] = sum[i] = 0;
ranges.clear();
node.clear();
}
void pushRange(int poz, int color, int x) {
if(ranges.back().color == color)
ranges.back().r = poz;
else
ranges.push_back(Range(poz, poz, color));
node.push_back(make_pair(x, color));
}
void mergeNodes(const vector<int> &r, const vector<int> &b) {
int i, j;
i = j = 0;
if(r[0] < b[0])
ranges.push_back({0, 0, 1});
else
ranges.push_back({0, 0, 2});
while(i < r.size() || j < b.size()) {
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
pushRange(i + j, 1, r[i]);
++i;
} else {
pushRange(i + j, 2, b[j]);
++j;
}
}
for(int i = 0; i < node.size(); ++i)
sum[i] = sum[i - 1] + node[i].first;
}
long long sumRange(int a, int b) {
return sum[b] - sum[a - 1];
}
void initDp() {
dp[-1] = 0;
for(int i = ranges[0].l; i <= ranges[0].r; ++i)
dp[i] = INF;
}
void doDp(int i) {
int l, r;
long long rgang = INF, lazy = 0LL, xdp = dp[ranges[i - 1].r];
priority_queue<pair<long long, int>,
vector<pair<long long, int> >,
std::greater<pair<long long, int> > > lgang;
l = ranges[i - 1].r;
r = ranges[i].l;
for(int j = ranges[i - 1].l; j <= ranges[i - 1].r; ++j)
lgang.push(make_pair(dp[j - 1] - sumRange(j, ranges[i - 1].r) + (long long)(ranges[i - 1].r - j + 1) * node[ranges[i].l].first, j));
while(r <= ranges[i].r) {
if(l >= ranges[i - 1].l) {
xdp = min(xdp, dp[l - 1]);
rgang = min(xdp - sumRange(l, ranges[i - 1].r), rgang - node[ranges[i - 1].r].first);
} else
rgang -= node[ranges[i - 1].r].first;
lazy -= node[ranges[i].l].first;
while(!lgang.empty() && ranges[i - 1].r - lgang.top().second <= r - ranges[i].l)
lgang.pop();
dp[r] = rgang + sumRange(ranges[i].l, r);
if(!lgang.empty())
dp[r] = min(dp[r], lgang.top().first + sumRange(ranges[i].l, r) + lazy);
--l;
++r;
}
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
initAll(r.size() + b.size());
mergeNodes(r, b);
initDp();
for(int i = 1; i < ranges.size(); ++i)
doDp(i);
return dp[node.size() - 1];
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'void mergeNodes(const std::vector<int>&, const std::vector<int>&)':
wiring.cpp:43:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < r.size() || j < b.size()) {
~~^~~~~~~~~~
wiring.cpp:43:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < r.size() || j < b.size()) {
~~^~~~~~~~~~
wiring.cpp:44:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
~~^~~~~~~~~~~
wiring.cpp:44:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
~~^~~~~~~~~~
wiring.cpp:44:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
~~^~~~~~~~~~
wiring.cpp:53:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < node.size(); ++i)
~~^~~~~~~~~~~~~
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:104:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < ranges.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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |