#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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
46 ms |
3520 KB |
Output is correct |
13 |
Correct |
102 ms |
3632 KB |
Output is correct |
14 |
Correct |
64 ms |
3492 KB |
Output is correct |
15 |
Correct |
62 ms |
2232 KB |
Output is correct |
16 |
Correct |
73 ms |
3560 KB |
Output is correct |
17 |
Correct |
91 ms |
3456 KB |
Output is correct |
18 |
Correct |
89 ms |
3452 KB |
Output is correct |
19 |
Correct |
101 ms |
3572 KB |
Output is correct |
20 |
Correct |
83 ms |
3492 KB |
Output is correct |
21 |
Correct |
89 ms |
3500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |