#include <iostream>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <vector>
#include <cmath>
int main()
{
//freopen("duotaA.txt", "r", stdin);
int K, N;
std::cin >> K >> N;
long long extra = 0;
std::vector<std::pair<int, int>> ranges;
for(int i = 0; i < N; i++)
{
int ai, bi;
char ac, bc;
std::cin >> ac >> ai >> bc >> bi;
if(ai > bi)
std::swap(ai, bi);
if(ac == bc)
extra += bi - ai;
else
ranges.push_back(std::make_pair(ai, bi));
}
N = ranges.size();
if(N == 0)
{
std::cout << extra;
return 0;
}
if(K == 1)
{
long long dist = 0;
int nums[N*2];
for(int i = 0; i < N; i++)
{
nums[i*2] = ranges[i].first;
nums[i*2+1] = ranges[i].second;
}
std::sort(nums, nums + N*2);
int point = nums[N];
for(int i = 0; i < N; i++)
{
if(point >= ranges[i].first && point <= ranges[i].second)
dist += 1 + ranges[i].second - ranges[i].first;
else dist += 1 + ranges[i].second - ranges[i].first + 2 * std::min(std::abs(ranges[i].second - point), std::abs(ranges[i].first - point));
}
std::cout << dist + extra;
}
else
{
// speju kad gali buti po puses masyvu mediana jeigu K == 2
long long dist = 0;
int nums[N*2];
for(int i = 0; i < N; i++)
{
nums[i*2] = ranges[i].first;
nums[i*2+1] = ranges[i].second;
}
std::sort(nums, nums + N*2);
int point1, point2;
point1 = nums[N/2];
point2 = nums[N/2 + N];
for(int i = 0; i < N; i++)
{
int temp1;
int temp2;
if(point1 >= ranges[i].first && point1 <= ranges[i].second)
temp1 = 1 + ranges[i].second - ranges[i].first;
else temp1 = 1 + ranges[i].second - ranges[i].first + 2 * std::min(std::abs(ranges[i].second - point1), std::abs(ranges[i].first - point1));
if(point2 >= ranges[i].first && point2 <= ranges[i].second)
temp2 = 1 + ranges[i].second - ranges[i].first;
else temp2 = 1 + ranges[i].second - ranges[i].first + 2 * std::min(std::abs(ranges[i].second - point2), std::abs(ranges[i].first - point2));
dist += std::min(temp1, temp2);
}
std::cout << dist + extra;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
23 ms |
2036 KB |
Output is correct |
13 |
Correct |
59 ms |
1884 KB |
Output is correct |
14 |
Correct |
35 ms |
1744 KB |
Output is correct |
15 |
Correct |
33 ms |
1228 KB |
Output is correct |
16 |
Correct |
37 ms |
2000 KB |
Output is correct |
17 |
Correct |
49 ms |
2100 KB |
Output is correct |
18 |
Correct |
47 ms |
1984 KB |
Output is correct |
19 |
Correct |
56 ms |
1988 KB |
Output is correct |
20 |
Correct |
42 ms |
1908 KB |
Output is correct |
21 |
Correct |
51 ms |
2000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |