제출 #842974

#제출 시각아이디문제언어결과실행 시간메모리
842974tch1cherinPalembang Bridges (APIO15_bridge)C++17
0 / 100
1 ms392 KiB
#include <bits/stdc++.h> using namespace std; int main() { int K, N; cin >> K >> N; vector<int> P(N), Q(N); vector<int> A, B; long long ans = 0, tot = LLONG_MAX; vector<int> D; for (int i = 0; i < N; i++) { char X, Y; cin >> X >> P[i] >> Y >> Q[i]; if (X > Y) { swap(X, Y); } if (X == Y) { ans += abs(P[i] - Q[i]); } else { D.push_back(P[i]); D.push_back(Q[i]); A.push_back(P[i]); B.push_back(Q[i]); } } P = A, Q = B; N = (int)P.size(); if (K == 1) { for (int x : D) { long long res = 0; for (int i = 0; i < N; i++) { res += abs(P[i] - x) + abs(Q[i] - x) + 1; } tot = min(tot, ans + res); } } else { for (int x : D) { for (int y : D) { long long res = 0; for (int i = 0; i < N; i++) { res += min(abs(P[i] - x) + abs(Q[i] - x), abs(P[i] - y) + abs(Q[i] - y)) + 1; } tot = min(tot, ans + res); } } } cout << tot << "\n"; }
#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...