이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
typedef long double ld;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int k, n;
cin >> k >> n;
vector <pair <int, int>> a;
ll res = 0;
vector <pair <int, int>> e;
vector <int> c;
ll cur = 0;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
char p, q;
int x, y;
cin >> p >> x >> q >> y;
res += abs(x - y);
if (x > y) swap(x, y);
if (p != q) {
e.push_back({x, -1});
e.push_back({y, 1});
a.emplace_back(x, y);
cur += 2 * x;
++cnt1;
++res;
}
c.push_back(x);
c.push_back(y);
}
sort(c.begin(), c.end());
c.resize(unique(c.begin(), c.end()) - c.begin());
sort(e.begin(), e.end());
assert(k == 1);
ll ans = cur + res;
for (int i = 0; i < (int)e.size(); ++i) {
if (i) {
cur -= 2LL * cnt1 * (e[i].first - e[i - 1].first);
cur += 2LL * cnt2 * (e[i].first - e[i - 1].first);
}
if (e[i].second == -1) --cnt1;
else ++cnt2;
ans = min(ans, res + cur);
}
cout << ans << '\n';
}
# | 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... |