# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1047834 | DeathIsAwe | Palembang Bridges (APIO15_bridge) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
int main() {
char p, q;
int s, t, k, n; cin >> k >> n;
ll ans = 0;
unordered_map<int,int> bruh;
unordered_map<int,int> sus;
vector<int> bridgeplaces;
vector<pair<int, int>> housework;
if (k == 2) {
return 0;
}
ll ahead = 0;
for (int i=0;i<n;i++) {
cin >> p >> s >> q >> t;
if (t > s) {
swap(t, s);
}
if (p == q) {
ans += abs(s - t);
continue;
}
ahead++;
housework.push_back(make_pair(t, s));
if (bruh.find(s) == bruh.end()) {
bruh[s] = 1;
if (sus.find(s) == sus.end()) {
bridgeplaces.push_back(s);
}
} else {
bruh[s]++;
}
if (sus.find(t) == sus.end()) {
sus[t] = 1;
if (bruh.find(t) == bruh.end()) {
bridgeplaces.push_back(t);
}
} else {
sus[t]++;
}
}
sort(bridgeplaces.begin(), bridgeplaces.end());
for (pair<int,int> i: housework) {
ans += i.first + i.second + 1;
}
ll curans = ans;
ll prev = 0;
ll visitednum = 0;
for (int i=0;i<bridgeplaces.size();i++) {
curans += 2 * (bridgeplaces[i] - prev) * (visitednum - ahead);
//cout << visitednum << ' ' << ahead << ' ' << bridgeplaces[i] << ' ' << curans << '\n';
if (bruh.find(bridgeplaces[i]) != bruh.end()) {
visitednum += bruh[bridgeplaces[i]];
}
if (sus.find(bridgeplaces[i]) != sus.end()) {
ahead -= sus[bridgeplaces[i]];
}
ans = min(ans, curans);
prev = bridgeplaces[i];
}
cout << ans;
}