이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// ...
// subtask 4.
#if not LOCAL
#define NDEBUG 1
#endif
#include<bits/stdc++.h>
int main(){
std::ios::sync_with_stdio(0);std::cin.tie(0);
int k, number; std::cin>>k>>number;
if(k==1 or number>1000) return 1;
int64_t result{};
std::vector<std::array<int, 2>> queries;
std::vector<int> positions;
for(int _=0; _<number; ++_){
char a, b; int first, sec;
std::cin>>a>>first>>b>>sec;
if(first>sec) std::swap(first, sec);
result+=sec-first; // minimum required
if(a!=b){
result+=1; // need to cross a bridge anyway
queries.push_back({first, sec});
positions.push_back(first); positions.push_back(sec);
}
}
number=-1;
std::sort(begin(positions), end(positions));
positions.erase(std::unique(begin(positions), end(positions)), end(positions));
auto minAdd=INT64_MAX;
for(auto y: positions){
std::vector<std::pair<int, int>> changes; // increases in slope. (pos, delta).
int64_t zeroValue{}; // value at zero.
// slope at zero is always 0.
// example: for function max(x, 0), changes={ {0, 1} }, zeroValue=0.
for(auto [l, r]: queries){
assert(l<=r);
if(y<l){
zeroValue+=l-y;
}else if(y>r){
auto const delta=std::min(l, y-r);
zeroValue+=delta;
changes.push_back({l-delta, -1});
changes.push_back({l, 1});
changes.push_back({r, 1});
}
}
changes.push_back({y, 0}); // for convenience
std::sort(begin(changes), end(changes), [&](auto const& first, auto const& sec){
return first.first<sec.first;
});
int64_t value=zeroValue; int last=0, slope=0;
for(auto [pos, delta]: changes){
assert(pos>=last);
assert(pos<=y);
value+=slope*(pos-last);
minAdd=std::min(minAdd, value);
last=pos;
if(pos==y) break;
slope+=delta;
}
assert(last==y);
}
std::cout<<result+minAdd<<'\n';
}
# | 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... |