이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <vector>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <unordered_map>
#include <functional>
#include <bitset>
#include <sstream>
using namespace std;
using int64 = long long;
int N, M, K;
vector<pair<int64, int64>> segments;
vector<int64> X;
vector<int64> vec;
map<int64, int> idx;
int64 solve1brute() {
int64 res = (1LL<<60);
for (auto & x : vec) {
int64 cur = 0;
for (auto & s : segments) {
if (s.second < x) {
cur += 2 * (x - s.second);
} else if (s.second >= x && s.first <= x) {
// nada
} else {
cur += 2 * (s.first - x);
}
}
res = min(res, cur);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64 res = 0;
cin >> K >> N;
set<int> uniq;
for (int i = 0; i < N; i++) {
char z1, z2;
int x1, x2;
cin >> z1 >> x1 >> z2 >> x2;
res += abs(x1 - x2);
if (z1 != z2) {
segments.emplace_back(min(x1, x2), max(x1, x2));
uniq.insert(x1);
uniq.insert(x2);
}
}
if (!size(segments)) {
cout << res << "\n";
return 0;
}
vec = vector<int64>(begin(uniq), end(uniq));
N = size(vec);
for (int i = 0; i < N; i++) {
idx[vec[i]] = i;
}
M = size(segments);
res += M; // every crossed a bridge
// need at least one bridge;
cout << res + solve1brute() << "\n";
return 0;
}
# | 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... |