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 <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... |