제출 #699348

#제출 시각아이디문제언어결과실행 시간메모리
699348CatalinTPalembang Bridges (APIO15_bridge)C++17
8 / 100
2085 ms27792 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...