이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "wiring.h"
using namespace std;
using ll = long long;
using pii = pair<ll, int>;
const int INF = 2e9;
void print (vector<pii> v)
{
for(auto i: v)
cout << i.first << ' ' << i.second << endl ;
cout << endl ;
}
void print (vector<ll> v)
{
for(auto i: v)
cout << i << ' ' ;
cout << endl ;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
vector<pii> ar;
for(auto i: r)
ar.push_back({i, 0});
for(auto i: b)
ar.push_back({i, 1});
sort(ar.begin(), ar.end());
ar.insert(ar.begin(), (pii){0, 0});
int acc = 0;
map<int, int> mfirst, mlast;
vector<long long> cost;
mfirst[0] = 0;
mlast[0] = 0;
cost.push_back(0);
for(int i = 1 ; i < ar.size() ; i++)
{
ar[i].second ? acc++ : acc--;
if(mlast.count(acc))
{
if(mlast[acc] + 2 == i)
cost.push_back(cost[mlast[acc]] + ar[i].first - ar[i-1].first);
else
cost.push_back(cost[mlast[acc]] + cost[i-1] - cost[mlast[acc] + 1] + ar[i].first - ar[mlast[acc] + 1].first);
mlast[acc] = i;
}
else
{
long long temp = INF;
if(ar[i].second == 0) //b
swap(b, r);
auto pt = lower_bound(r.begin(), r.end(), ar[i].first);
if(pt != r.end())
temp = *pt - ar[i].first ;
if(pt != r.begin())
temp = min(temp, ar[i].first - *prev(pt));
if(ar[i].second == 0)
swap(b, r);
cost.push_back(cost[i-1] + temp);
mlast[acc] = mfirst[acc] = i ;
}
}
//print(ar);
//print(cost);
return cost.back();
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i = 1 ; i < ar.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... |