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;
vector<int> cnt;
map<int64, int> idx;
int64 solve1brute() {
int64 res = (1LL<<60);
for (int i = 0; i < N; i++) {
int64 cur = 0;
for (auto & s : segments) {
if (s.second < vec[i]) {
cur += 2 * (vec[i] - s.second);
} else if (s.second >= vec[i] && s.first <= vec[i]) {
// nada
} else {
cur += 2 * (s.first - vec[i]);
}
}
res = min(res, cur);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64 res = 0;
cin >> N >> K;
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;
}
vector<int> vec(begin(uniq), end(uniq));
N = size(vec);
for (int i = 0; i < N; i++) {
idx[vec[i]] = i;
}
for (auto & s : segments) {
cnt[s.first]++;
cnt[s.second]--;
}
M = size(segments);
// need at least one bridge;
cout << 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... |