이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pii pair<int, int>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
const int MAXN = (int)2e5 + 5;
const int INF = 1e18;
pair <char, int> st[MAXN];
pair <char, int> fn[MAXN];
bool ok(int pos) {
return (st[pos].first == fn[pos].first);
}
vector <int> vec;
int check(int pos) {
int sum = 0;
for (int el : vec) {
sum += abs(el - pos);
}
return sum;
}
signed main() {
fastInp;
int n, k;
cin >> k >> n;
int side = 0;
int cnt = 0;
for (int i = 1; i <= n; i++) {
cin >> st[i].fr >> st[i].sc >> fn[i].fr >> fn[i].sc;
if (ok(i)) {
side += abs(st[i].sc - fn[i].sc);
} else {
cnt++;
vec.pb(st[i].sc);
vec.pb(fn[i].sc);
}
}
sort(all(vec));
if (k == 1) {
int opt = vec[szof(vec) / 2 - 1];
cout << side + check(opt) + cnt;
} else {
int ans = INF;
for (int i = 0; i < szof(vec); i++) {
for (int j = 0; j < szof(vec); j++) {
int cur = 0;
for (int p = 1; p <= n; p++) {
int x = st[p].sc;
int y = fn[p].sc;
if (ok(p)) {
cur += abs(x - y);
}
if (!ok(p)) {
cur += 1 + min(abs(vec[i] - x) + abs(vec[i] - y), abs(vec[j] - x) + abs(vec[j] - y));
}
}
ans = min(ans, cur);
}
}
}
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# | 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... |