#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxN = 1e5+1;
int k, n;
struct Tbridge
{
char a, b;
ll x, y;
bool chk;
}p[maxN];
ll ans = 0;
void Input()
{
vector<ll> v;
cin >> k >> n;
for(int i = 1; i <= n; i++)
{
cin >> p[i].a >> p[i].x >> p[i].b >> p[i].y;
if(p[i].a == p[i].b)
{
ans += abs(p[i].y - p[i].x);
p[i].chk = false;
}
else
{
v.pb(p[i].x);
v.pb(p[i].y);
p[i].chk = true;
}
}
sort(v.begin(), v.end());
// v.erase(unique(v.begin(), v.end()), v.end());
if(k == 1)
{
if(v.empty())
{
cout << ans;
return;
}
ll res = (int)v.size() / 2;
ll h = v[res];
for(int i = 1; i <= n; i++)
if(p[i].chk)
ans += abs(h - p[i].x) + abs(h - p[i].y) + 1;
cout << ans;
return;
}
if(v.empty())
{
cout << ans;
return;
}
if(v.size() == 1)
{
ll h = v[0];
for(int i = 1; i <= n; i++)
if(p[i].chk)
ans += abs(h - p[i].x) + abs(h - p[i].y) + 1;
cout << ans;
return;
}
if(v.size() == 2)
{
ll h = v[0], h2 = v[1];
for(int i = 1; i <= n; i++)
if(p[i].chk)
ans += min(abs(h - p[i].x) + abs(h - p[i].y) + 1, abs(h2 - p[i].x) + abs(h2 - p[i].y) + 1);
cout << ans;
return;
}
ll res = v.size()/3;
ll h = v[res], h2 = v[res*2];
for(int i = 1; i <= n; i++)
if(p[i].chk)
ans += min(abs(h - p[i].x) + abs(h - p[i].y) + 1, abs(h2 - p[i].x) + abs(h2 - p[i].y) + 1);
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
Input();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
# |
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 |
20 ms |
5068 KB |
Output is correct |
13 |
Correct |
39 ms |
5188 KB |
Output is correct |
14 |
Correct |
28 ms |
4668 KB |
Output is correct |
15 |
Correct |
22 ms |
3152 KB |
Output is correct |
16 |
Correct |
27 ms |
5136 KB |
Output is correct |
17 |
Correct |
27 ms |
5120 KB |
Output is correct |
18 |
Correct |
33 ms |
5068 KB |
Output is correct |
19 |
Correct |
38 ms |
5128 KB |
Output is correct |
20 |
Correct |
26 ms |
5116 KB |
Output is correct |
21 |
Correct |
34 ms |
5080 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 |
0 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 |
1 ms |
260 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 |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |