This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main()
{
size_t n, k;
cin >> k >> n;
vector<long> home, work;
vector<long> positions;
long cost = 0;
for (size_t i = 0; i < n; i++)
{
char p, q;
long s, t;
cin >> p >> s >> q >> t;
if (p != q)
{
home.push_back(s);
work.push_back(t);
positions.push_back(s);
positions.push_back(t);
}
else
{
cost += abs(s - t);
}
}
if (positions.empty())
{
cout << cost << '\n';
return 0;
}
sort(positions.begin(), positions.end());
if (k == 1)
{
long median = positions[positions.size() / 2];
for (size_t i = 0; i < home.size(); i++)
{
cost += abs(home[i] - median) + abs(work[i] - median) + 1;
}
}
else
{
long third = positions[positions.size() / 3];
long two_thirds = positions[positions.size() * 2 / 3];
for (size_t i = 0; i < home.size(); i++)
{
cost += min(abs(home[i] - third) + abs(work[i] - third),
abs(home[i] - two_thirds) + abs(work[i] - two_thirds)) +
1;
}
}
cout << cost << '\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... |