//In the name of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e5 + 5;
int n, k, med[N][2], med2[N][2], p, q;
ll base;
char ch1, ch2;
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int> > q2;
priority_queue<int> q3;
priority_queue<int, vector<int>, greater<int> > q4;
vector<pair<ll, pair<int, int> > > ve;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> k >> n;
for(int i = 0; i < n; i++)
{
cin >> ch1 >> p >> ch2 >> q;
if(ch1 != ch2)
{
ve.pb({p + q, {min(p, q), max(p, q)}});
base ++;
}
else
base += abs(p - q);
}
sort(all(ve));
for(ll i = 0; i < (ll)ve.size(); i++)
{
auto x = ve[i];
q1.push(x.S.F);
q2.push(x.S.S);
while(q1.top() > q2.top())
{
int to1 = q1.top(), to2 = q2.top();
q1.pop();q2.pop();
q1.push(to2);
q2.push(to1);
}
med[i][0] = q1.top();
med[i][1] = q2.top();
}
//cout << med[ve.size() - 1][0] << " " << med[ve.size() - 1][1] << endl;
//if(!n)
//cout << base << endl;
if(!ve.size())
{
cout << base << endl;
return 0;
}
if(k == 1|| ve.size() == 1)
{
ll ans1, ans2;
ans1 = ans2 = 0;
for(int i = 0; i < ve.size(); i++)
{
ans1 += abs(ve[i].S.F - med[ve.size() - 1][0]);
ans1 += abs(ve[i].S.S - med[ve.size() - 1][0]);
ans2 += abs(ve[i].S.F - med[ve.size() - 1][1]);
ans2 += abs(ve[i].S.S - med[ve.size() - 1][1]);
}
cout << min(ans1, ans2) + base << endl;
}
else
{
ll ans = 1e18 + 5;
for(int i = ve.size() - 1; i >= 0; i--)
{
auto x = ve[i];
q3.push(x.S.F);
q4.push(x.S.S);
while(q1.top() > q4.top())
{
int to1 = q3.top(), to2 = q4.top();
q3.pop();q4.pop();
q3.push(to2);
q4.push(to1);
}
med2[i][0] = q3.top();
med2[i][1] = q4.top();
}
for(int i = 0; i < ve.size() - 1; i++)
{
ll ans1, ans2, ans3, ans4;
ans1 = ans2 = ans3 = ans4 = 0;
for(int j = 0; j <= i; j++)
{
ans1 += abs(ve[j].S.F - med[i][0]);
ans1 += abs(ve[j].S.S - med[i][0]);
ans2 += abs(ve[j].S.F - med[i][1]);
ans2 += abs(ve[j].S.S - med[i][1]);
}
for(int j = i + 1; j < ve.size(); j++)
{
ans3 += abs(ve[j].S.F - med2[i + 1][0]);
ans3 += abs(ve[j].S.S - med2[i + 1][0]);
ans4 += abs(ve[j].S.F - med2[i + 1][1]);
ans4 += abs(ve[j].S.S - med2[i + 1][1]);
}
ans = min(ans, min(ans1, ans2) + min(ans3, ans4));
}
ll ans1, ans2;
ans1 = ans2 = 0;
for(int i = 0; i < ve.size(); i++)
{
ans1 += abs(ve[i].S.F - med[ve.size() - 1][0]);
ans1 += abs(ve[i].S.S - med[ve.size() - 1][0]);
ans2 += abs(ve[i].S.F - med[ve.size() - 1][1]);
ans2 += abs(ve[i].S.S - med[ve.size() - 1][1]);
}
ans = min(ans, min(ans1, ans2));
cout << ans + base << endl;
}
}
/*
1 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7
2 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7
*/
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:73:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < ve.size(); i++)
| ~~^~~~~~~~~~~
bridge.cpp:102:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 0; i < ve.size() - 1; i++)
| ~~^~~~~~~~~~~~~~~
bridge.cpp:113:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int j = i + 1; j < ve.size(); j++)
| ~~^~~~~~~~~~~
bridge.cpp:126:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for(int i = 0; i < ve.size(); i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
30 ms |
3948 KB |
Output is correct |
13 |
Correct |
57 ms |
3944 KB |
Output is correct |
14 |
Correct |
48 ms |
3688 KB |
Output is correct |
15 |
Correct |
33 ms |
2544 KB |
Output is correct |
16 |
Correct |
33 ms |
3956 KB |
Output is correct |
17 |
Correct |
52 ms |
3944 KB |
Output is correct |
18 |
Correct |
40 ms |
3944 KB |
Output is correct |
19 |
Correct |
61 ms |
3948 KB |
Output is correct |
20 |
Correct |
43 ms |
3948 KB |
Output is correct |
21 |
Correct |
57 ms |
4116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |