This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 ll N = 1e5 + 5;
ll n, k, med[N][2], base, med2[N][2];
vector<pair<ll, pair<ll, ll> > > ve;
int main()
{
priority_queue<ll> q1;
priority_queue<ll, vector<ll>, greater<ll> > q2;
cin >> k >> n;
for(ll i = 0; i < n; i++)
{
char ch1, ch2;
ll p, q;
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())
{
ll 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(ll 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;
while(q1.size())q1.pop();
while(q2.size())q2.pop();
for(ll i = 0; i < (ll)ve.size(); i++)
{
auto x = ve[ve.size() - i - 1];
q1.push(x.S.F);
q2.push(x.S.S);
while(q1.top() > q2.top())
{
ll to1 = q1.top(), to2 = q2.top();
q1.pop();q2.pop();
q1.push(to2);
q2.push(to1);
}
med2[i][0] = q1.top();
med2[i][1] = q2.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(ll 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 (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:68:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(ll i = 0; i < ve.size(); i++)
| ~~^~~~~~~~~~~
bridge.cpp:99:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int i = 0; i < ve.size() - 1; i++)
| ~~^~~~~~~~~~~~~~~
bridge.cpp:110:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(int j = i + 1; j < ve.size(); j++)
| ~~^~~~~~~~~~~
bridge.cpp:123:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(ll i = 0; i < ve.size(); i++)
| ~~^~~~~~~~~~~
# | 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... |